Wednesday 10 September 2014

(Abstraction, Encapsulation, Inheritance & Polymorphism) OOPS concepts in Java.



Inheritance:
 Inheritance is one of the key features of Object Oriented Programming. Inheritance provided mechanism that allowed a class to inherit property of another class
. When a Class extends another class it inherits all non-private members including fields and methods. Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as Super class (Parent) and Sub class (child) in Java language.
To promote code reuse
 
Class Parent
{
 public void p1()
  {
      System.out.println("Prent method");
   }}
public class child extends Parent{
  public void c1()
    {
     System.out.println("Child method");
    }
public static void main(string[] args)
{
  Child cobj=new child();
  cobj.c1();
  cobj.p1();

}
}

Output-child method
       Parent method
 

Encapsulation in Java:-

Encapsulation is a practice to bind related functionality (Methods) & Data (Variables) in a protective wrapper (Class) with required access modifiers (public, private, default & protected) so that the code can be saved from unauthorized access by outer world and can be made easy to maintain.

We can achieve complete encapsulation in java by making members of a class private and access them outside the class only through getters and setters.
public class EncapsulationExample
{
private String Name;
private String Address;
private double salary;
}
 
We cannot access private member variable

public class Mairnfunction extends EncapsulationExample
       public static void main(String[] args)
       {
              Mairnfunction m=new Mairnfunction("ritesh","spa",23.33);
              //m.salary=344;
}
Polymorphism means many behaviour
  • Polymorphism means "one interface, many possible implementations." 
  • Polymorphism is a Greek word that means Many Shapes.

 
public class Animal {
    public void makeNoise()
    {
        System.out.println("Some sound");
    }
}

class Dog extends Animal{
    public void makeNoise()
    {
        System.out.println("Bark");
    }
}

class Cat extends Animal{
    public void makeNoise()
    {
        System.out.println("Meawoo");
    }
}
Now which makeNoise() method will be called, depends on type of actual instance created on runtime e.g.
public class Demo
{
    public static void main(String[] args) {
        Animal a1 = new Cat();
        a1.makeNoise(); //Prints Meowoo
         
        Animal a2 = new Dog();
        a2.makeNoise(); //Prints Bark
   
}
For example,
  • Cars are designed with polymorphism in mind.
  • They all use the same interface-an accelerator pedal-to change speed, even though the internal method may differ from model to model.
  • The auto industry designs cars this way so that the drivers do not have to learn a new interface for each new model of car.
    1.      In java, polymorphism is divided into two parts : method overloading and method overriding.

Abstraction

Show functionality hide complexity
Abstraction in Object Oriented Programming helps to hide the irrelevant details of an object
to make a summary of or from (a piece of writing, a speech, etc.); summarize:
y using abstraction, we can separate the things that can be grouped to another type.
public class Car {
    int price;
    String name;
    String color;
    int engineCapacity;
    int engineHorsePower;     
    String wheelName;
    int wheelPrice;   
    void move(){
    //move forward  
    }
    void rotate(){
      //Wheels method
    }   
    void internalCombustion(){
      //Engine Method
    }
    }
In the above example, the attributes of wheel and engine are added to the Car type. As per the programming, this will not create any kind of issues. But when it comes to maintenance of the application, this becomes more complex.

Summary:

Abstraction is one of the fundamental principles of Object Oriented Programming languages. 
It helps to reduce the complexity and also improves the maintainability of the system. When combined with the concepts of the Encapsulation and Polymorphism, Abstraction gives more power to the Object oriented programming languages.
ABSTRACTION
* Show functionality hide complexity
*Abstraction implements through abstract class and interface
*If  any class has any abstract method then it can not be instantiated
*Abstract method can not be used in normal class ,but used in abstract class
*Abstract class may have abstract functions.
*Any child class has to override all the abstract class's abstract function othrwise child has to   
  become abstract.
*We never call the abstract function explicitly.automatically runs the code of the abstract 
     function.
* It is not mandatory that abstract class should have atleast one abstract function 
* Cant mark class as abstract and final both ....both have opposite meaning
Different between Method over loading and method over ridding

Method over loading:-for example:Below two function with same name, when we call through main function, compiler will see the two function with same name but only different between parameter , so it will call first method. Constractor overloading is work with same.

  Class  A

public void show()
{
System.out.println("show parent");

}

public void show(int i)
{
System.out.println("show parameter");

}
public static void main(args[] strgs)
{
A obj=new A();
obj.show();
}
Method over ridding:-Here is two class A and B, when we create and object of class B and call show method it is called class B method because both have same method, it is going to give the preference class B method, it is called over ridding because show method is overridded


  Class  A

public void show()
{
System.out.println("show parent");

}

public void show(int i)
{
System.out.println("show parameter");

}

Class B extends A
{

public void show()
{
System.out.println("show parent");

}
public static void main(args[] strgs)
{
B obj=new B();
obj.show();
}

Another example:-
Class Figure
{
public void draw()
{
System.out.println("show draw figure");
 }

Class Rectangle extends Figure

public void draw()
{
System.out.println("show draw rectangle");
 }

public static void main(args[] strgs)
{
Figure obj=new Rectangle ();
obj.draw();
}
 Output:- It is print draw method of Rectangle class


No comments:

Post a Comment