OOP (Object Oriented Programming) In Java

OOP (Object Oriented Programming) In Java :

Context

  • Principles of java
  • Java is an OOP language?
  • What is Object in java?
  • Why use an OOP?
  • Types of polymorphism in java?
  • Why overriding is called run time polymorphism?

Object

  • Instance of object class.
  • Real time entity.
  • Object is contains variable and function (method).
  • Runtime entities.
  • Object take up space in memory and have an associated address like a record in Pascal or structure in C.
  • Object created new keyword.
  • An object is a region of storage that defines state & behavior.
    1) State is represented by a set of variables & the values they contain.
    2) Behavior is represented by a set of variables & the logic they implement.
  • When we allocate a memory to class definition, an object created in memory (RAM).

class

Data and methods are wrapped into a single unit which is known as class

OOP (Object Oriented Programming) In Java

Why use an OOP?

OOP is use complex program is divided modules like member variables and methods.

Object Oriented Programming

1) Encapsulation
2) Inheritance
3) Polymorphism

1) Encapsulation

  • Wrapping up of data and methods into a single unit (called class) is known as encapsulation.For example: Person, School, Animal, Company.

Programming language definition

It is a process of grouping data and instructions which operates on the data within a single entity. Wrapping of data and instruction is called encapsulation.

  • Encapsulation is the process of hiding information details and protecting data and behavior of an object from misuse by other objects. In Java, encapsulation is done using access modifiers (protected, private) with classes, interfaces, setters, getters.
  1. private visibility offers full encapsulation.
  2. protected and default offer limited encapsulation.
  3. public no encapsulation.
  4. Provide the public setter and getter methods in the class to set/modify the values of the variable/fields.
  • Set the instance variables private so that these private variables cannot be accessed directly by other classes. Set getter and setter methods of the class as public so that we can set and get the values of the fields.

For example:

  1. If your code doesn’t depend on parts of your program that are likely to change. When you change your program, they don’t have to change their code.
  2. Suppose you have an account in the bank. If your balance variable is declared as a public variable in the bank software, your account balance will be known as public, In this case, anyone can know your account balance. So, would you like it? Obviously No.
public class Account {
private double balance;
public double getbalance() {
return balance;
    }
}

3. When you enter the password for logging, they are retrieved in an encrypted form and verified and then you are given the access to your account. You do not have control over it that how the password has been verified. Thus, it keeps our account safe from being misused.

class Stats {
    private int weight;

publicintgetWeight() //Getter method to get weight.
    {
        return weight;
    }

public void setWeight(int w) //Setter method to set weight.
    {
        if (w < 0) {
System.out.println("Please enter weight greater than zero");
        } else {
            weight = w;
        }
    }

    public static void main(String... ar) {
        Stats ob = new Stats();
ob.setWeight(100);
    }
}
public class Rectangle {
    // member variables are private, then we implement setter & getter
    private double height;
    private double width;

    /**
     * @return the height
     */
    public double getHeight() {
        return height;
    }

    /**
     * @param height the height to set
     */
    public void setHeight(double height) {
this.height = height;
    }

    /**
     * @return the width
     */
    public double getWidth() {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(double width) {
this.width = width;
    }

    public double getArea() {
        return this.height * this.width;
    }

    public void display() {
System.out.println("Height: " + this.height);
System.out.println("Width: " + this.width);
System.out.println("Area: " + getArea());
    }
}

2) Inheritance

  • One class acquires the properties and functionalities of another class.
  • The property of (data and method) super class is reused by sub class is knows as inheritance.
  • Inheritance is process which allows a Child class to inherit some properties from its parent class.
  • Hierarchical classification support.
  • To create sub class from super class the keyword “extends” is used.
  • Only properties with access modifier public and protected can be accessed in child class.
  • Class cannot access more than one class at a time but interface can implement any number of interfaces.

Advantage

Inheritance is that, code in base class need not be rewritten in the derived class.

3) Polymorphism

  • Ability to take more than one forms by a single entity.
  • When one task is performed by different ways i.e. known as polymorphism.
  • A single function name can be used to handle different no and different type of argument.
  • Polymorphism manifests itself in Java in the form of multiple methods having the same name.
  • “One interface and multiple implementation” means one function is used in different shape.
  • Inheritance, Overloading and Overriding are used to achieve polymorphism.
  • All non-static methods are by default “virtual functions.” Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are not inherited, are non-virtual.

Type of polymorphism

1)Compile time polymorphism or static binding or early binding

2) Run time polymorphism or dynamic binding or late binding.

  • Compile time polymorphism
  • Function (method) Overloading – within same class more than one method having same name but differing in signature.
  • Compile time polymorphism or static method dispatch is a process in which a call to an overloading method is compile time rather than at run time. In this process, we done overloading of methods is called through the reference variable of a class here no need to super class.
  • Runtime time polymorphism
  • Runtime polymorphism is done using inheritance and interface.
  • Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

abstraction

  • interfaces, classes, abstract classes, fields, methods and variables everything is an abstraction.
  • While encapsulation helps in hiding the data (instance variables) of a class from an illegal-access, abstraction provides essential general features without providing implementation details, by using an abstract class or an interface.
  • Abstraction is done when we need to only inherit from a certain class, but do not need to instantiate objects of that class.
  • Abstraction focuses on the outside view of an object (i.e. the interface). Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented
interface Food {
    public void cook();
}

class Soup implements Food {
    public void cook() {
System.out.println("Cooking Soup");
    }
}

class Pasta implements Food {
    public void cook() {
System.out.println("Cooking Pasta");
    }
}
  • We have created an interface Food and we have also provided the method signature of its method cook() and it’s left upon the implementer of this interface to provide an implementation of method cook() as he/she likes.
  • A class Soup implements interface Food by provides an implementation of its method cook().
  • class Pasta implements interface Food and provides an implementation of its method cook() as it likes.

Question :

  • Why overriding is called run time polymorphism?
  • Subclass methods will be invoked at runtime.
  • Sub class object and subclass method overrides the Parent class method during runtime. Hence it is called runtime polymorphism.
  • Overridden functions have the signature, and are implemented in different derived classes.
  • When an overridden method is called, it’s called is dependent on which object is doing the calling which is unknown at compile time.
  • The difference between abstraction and encapsulation?
  • Outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in