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 ?

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:

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 .

Subscribe via email

About Me

Mobile NO : 7503454801
Email address : manojbharal2@gmail.com
About Blog
Blog consist of posts on java j2ee technical topics. It covers all range of post starting with basic java questions up to advanced j2ee architecture. I am owning this blog. Content being published here is authentic and original . This is growing blog and visitors can expect huge relevant and useful information to enhance their knowledge on subject.
About me
I have total of around 8 years of experience in java j2ee technologies and am working on multiple business domain technical projects in all range of industries .