Sunday, September 22

Java Polymorpihsm , OOPS design principal , Dynamic binding , Overriding

                   Java Polymorpihsm , OOPS design principal , Dynamic binding , Overriding





What is polymorphism?

Ability of an java object to take more than one form is polymorphism.

What does that mean?

Let's take an practical example to understand this 

Say we have an living being called animal  who is a horse .Now I can say

Horse is running  

 OR
Animal is running


both are correct .Thus single living being is taking two form here


 1)Horse 2)Animal

Let's understand this by code

public class Animal{

}

public class Horse extends Animal{
}

public class Race {

public static void main(String args[]){

//here we can write

Animal animal =new Animal();

//as well as

Horse animal =new Horse();
Animal animal =new Horse();

}

}
So animal reference in above example is representing both Animal as well as Horse . Both are correct.

So animal reference of Animal class is taking more than one actual form here . This is polymorphism . Polymorphism is widely used in java and OO programming.Parent class or implemented interface reference is used to refer the subclass instance.


So is there any direct way to identify If an reference is polymorphic or not ?

There is straight formula to identify that

If a object pass more than one IS A relationship , It is polymorphic.


Horse animal =new Horse();
Animal animal =new Horse();


Horse instance above can be represented with Horse class reference as well as Animal class reference . Thus horse instance here depicts the polymorphic behavior.

Lets take another example :

public interface Politician{}
public class Minister{}
public class FinanceMinister extends Minister implements Politician{}
Now, the FinanceMinister class is considered to be polymorphic since this has multiple inheritance. Following are true for the above example:

  1.     A FinanceMinister IS-A Minister
  2.     A FinanceMinister IS-A Politician
  3.     A FinanceMinister IS-A FinanceMinister
  4.     A FinanceMinister IS-A Object

SO all below declarations are correct


FinanceMinister fm = new FinanceMinister ();
Minister m = fm;
Politician p = fm;
Object o = fm



All the reference variables fm,m,p,o refer to the same FinanceMinister object in the heap.;



-->



Polymorphism is extensively used in other OO concept overriding.

For example :


public class Politician {

void contest(){
}

}

public class Candidate extends Politician {

void contest(){
}

}

public class Election {


public static void main (String args[])
{

Politician polit=new Candidate()

Candidate polit1=new Candidate();

polit.contest();
polit1.contest();

}
}
Both method invocation polit.contest() and polit1.contest() invoke the contest method of Candidate Class .

Why?

because Candidate class is depicting polymorphic behavior here

It can be referred by Candidate class reference as well as Politician class reference and also Object class reference.

Although compiler looks for contest() method in both the classes separately for both statements to compile successfully. But at run-time It is decided based on the instance type , method of which class should be called.

Both reference refer to the instance of Candidate class so JVM invokes method of Candidate class in both the cases .

Thus polymorphism helps in achieving dynamic behavior at run-time.













No comments:

Post a Comment