Java Oops

There are few things you must keep in mind if you are learning java,Object Oriented Programming  (oop),
these are some features which have mentioned below
  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation
Object : Any physical and logical substance like shirt, jeans, attitude,behavior is called Object. now in technical word "Object is the instance of Class "  .
e.g
class Bicycle { // declaration  of class Bicycle.
 int gear = 1; // initialization local variable  gear.

}
Bicycle obj= new Bicycle(); // declaration of Object of Bicycle obj

Remark :  now you can get all the property, variable,function and method via this object

Class : A class is the blueprint from which individual objects are created.
e.g
class Priyanka{   // declaration of class
public static void main(String[] args) { //  main method invoke
 System.out.println("She is good lady. ");
}

Inheritance: Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses.
e.g
class MountainBike extends Bicycle { //  Bicycle extend to MountainBike method , variables by used of Extends keyword .
System.out.print("My first Bike");
}
Remark : This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique.

 Polymorphism: Polymorphism is the ability of an object to take on many forms.There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

Method Overloading:A class have multiple methods by same name but different parameters, it is known as method Overloading
e.g.
class Total{
  void add(int a,int b){System.out.println(a+b);}
  void add(int a,int b,int c){System.out.println(a+b+c);}

  public static void main(String args[]){
  Calculation obj=new Calculation();
  obj.add(10,10,10);
  obj.add(20,20);

  }  
}
Method Overriding: A child class has the same method as declared in the parent class, it is known as method overriding..
class Vehicle{
  void run(){System.out.println("Vehicle is running");}
}
class Bike extends Vehicle{

  public static void main(String args[]){
  Bike obj = new Bike();
  obj.run();
  }
}
Abstraction : Abstraction is process of hiding the implementation details and showing only the functionality to the user .

Encapsulation :Encapsulation is process of wrapping code and data together into a single unit.
POJO class is the good example of encapsulated class.


  Advantages to encapsulation.

 - Encapsulated Code is more flexible and easy to change with new requirements.
 - By providing only getter and setter method access, you can make the class read only.
 - Encapsulation in Java makes unit testing easy.
 - A class can have total control over what is stored in its fields. Suppose you want to set the value of marks field i.e. marks should be positive value, than you can write the logic of positive value in setter method.
 - Encapsulation also helps to write immutable class in Java which are a good choice in multi-threading environments.
 - Encapsulation allows you to change one part of code without affecting other part of code



Comments