Simple Example of Polymorphism in Java

Polymorphism is a very interesting Object Oriented Concept. Let me explain Polymorpshims in Java with a simple example.

What does Polymorphism mean?

Poly stands for many. And morph stand for shape. Thus polymorphism simply mean many shapes.

If you wonder many shapes of what? In the context of object orientation it means many shapes of the same function. I have covered object orientation concepts as a story in another post. Please read that, it will give you an overview.

We are going to take that example here and elaborate with the code.

Let us assume :

There is a Dad class and Son class. Son class is derived from Dad class.

Dad class has a function called whoAreYou(). Son class overrides this function and also has the function whoAreYou(). Now the same function will return different result based on the context. That is Polymorphism.


Dad class:

Son class:

Main class:




That is a simple example of polymorphism in Java.

Note: Java programmers are told that there are two ways to achieve polymorphism – overriding and overloading. Overloading means having the same method name with different argument types or different number of arguments. Polymorphism by overloading is contested. When you have same method name but different argument, eventually is it a totally different function. However, some programmers call it a static polymorphism. When you are designing the object model, you don’t have to worry about this debate. But good to know for theoretical knowledge. You may want to check out here for more details on this.

Click Here to Leave a Comment Below

Leave a Reply: