Polymorphism in Java

In the previous post, we have seen Initializer Block.In this post , we will see what is polymorphism in Java. In object oriented programming, we know it is best at providing the reusability of the code. Polymorphism concept also contribute in providing reusabiliy. We will see it in details.

What is the significance of name polymorphism?

Poly and morphe  are 2 greek words that have meaning many and form respectively. Polymorphism is combination of these 2 words that means manyform i.e. something which can be used in many form.

Polymorphism allows us to have multiple implementation of one object or method(that may have the same name). Computers differentiates between them by the method signatures (the list of parameters: their number, their types, and the order of the types.)

There are 2 Forms of polymorphism :

  1. Compile time polymorphism or also called early binding
  2. Run time polymorphism or also called late binding

Suppose, there are 2 methods which are A() and A(int b). Now, after compiling the code into byte code compiler knows which method to execute. Since, method name is same but parameter list is different known as method Overloading and it happens at compile time, that’s why known as Compile Time Polymorphism.

Consider, there are 2 methods whose name and parameter list are same, but implementation or behavior is different i.e. A(){ //buy a book}  and A(){// buy a laptop}. This is known as method overriding and as computer does’t understand which one to execute at compile time, So their binding is resolved at run time which is known as Run Time Polymorphism. Sometimes,run-time polymorphism is referred to as dynamic binding as well.


Let’s focus on Run Time Polymorphism in this post. In this process, a reference variable of superclass is created which points to child class object.It is also known as upcasting.

Understand with an example given below.

Polymorphism can be applied to methods of the class. Let’s understand with some examples.

We have one class called Machine and another child class Printer. A reference variable of superclass i.e. Machine points to the object of child class which is Printer.

Output: Starts a printer

Can we use run time polymorphism with data-members of the class?

We will take the same classes as above and declare a boolean variable called ‘start’ which will tell the status of machine i.e. true or false.We make a reference variable of parent class which points towards the child object Printer.But it prints the output from the parent class ,that means polymorphism does not apply on data members of class.

Output: Running : false
Note: Run time polymorphism is not applicable on data members of the class.

What if there is multilevel inheritance?

Let’s see how to apply polymorphism if there is multilevel inheritance involved.

Starts a machine
Starts a Laptop
Starts a Desktop

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.


Initialization block in java
Abstract class and Interface
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *