What is polymorphism?
Ability of an java object or function 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:
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.;
Ability of an java object or function 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:
- A FinanceMinister IS-A Minister
- A FinanceMinister IS-A Politician
- A FinanceMinister IS-A FinanceMinister
- 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.;
No comments:
Post a Comment