Polymorphism in java with example and clear understanding

Hi in this post we will see what is polymorphism in java. I think this is one of favorite interview question as well. Let me start with the definition of polymorphism then we will see example.

polymorphism definition:

property of object to take many different form depending on the type of reference used to access the object.(do not worry it will be clear when we will see example). To be more precise Java object may be accesses using a reference with the same type as the object, a reference that is superclass of the object or a reference of interface that the object implement directly or through a superclass.

Regardless of the type of reference you have for the object in memory, the object itself does not change.

The type of object determines which properties exist within the object in memory.

The type of reference to the object determine which methods and variable are accessible.

Now lets see example. This example is very simple and properly commented. Still if you have any question please do write down your views in comments.



Hope this example clear your understanding of polymorphism. If you have any doubt or any suggestion please write down to me in comments.

Comments are closed.