Understating of Polymorphism in Java

By | September 21, 2018

This tutorial talks about Understating of  Polymorphism in Java.This is one of the important Oops feature in object oriented programming language.

Polymorphism is one of the four fundamental Oops concepts. The other three are-

  1. Encapsulation
  2. Inheritance
  3. Abstraction

Polymorphism  concept

Polymorphism is a Greek word, where poly means many and morph means change, refers to the ability of an object taking many forms.
The concept of polymorphism can be expressed as “One interface, multiple methods”. Where one general class can have the generic method and the derived classes (classes which extend the general class) may add class specific implementation to that method. At runtime or execution time “One interface” i.e. the general class will take “many forms” i.e. references of the derived classes.
You should Note that Polymorphism is extensively used with inheritance where parent class methods are overridden by the child classes.

Polymorphism in Java

There are basically two types of polymorphism in java –

  • Compile time polymorphism (or Static polymorphism)
  • Runtime polymorphism (or Dynamic Polymorphism)

We can perform polymorphism in java by:

 Method overloading (Compile time polymorphism) In method overloading it is known at compile time itself which of the overloaded method will be called.

Method overriding (Runtime polymorphism) In method overriding it is resolved at run time which of the overridden method would be called. The decision is made based on the object reference the reference variable is pointing to.

Let’s see an example of run time polymorphism in Java to understand the concept of polymorphism.

 

Here we have a parent class Shape with a method area(), Shape class has two child classes Square and Circle.
In Square class, area() method is overridden and it provides the implementation to calculate area of a square.
Same way area() method is overridden in Circle class to provide implementation to calculate area of a circle.
Notice that in PolymorphicClientTest class’ main method object shape of class Shape is declared and later that shape object takes the reference of square and then circle and calls the area method of the respective classes.

Output after running this program would be –

So, it can be seen that the shape object holds the reference of the square object initially thus the area method of the Square class is called. Later the shape object holds the reference of the Circle object thus the area method of the Circle class is called.

Some of the key points about Polymorphism –

  • As we know that Polymorphism is a Greek word, where poly means many and morph means many forms thus it refers to the ability of an object taking many forms.
  • Many classes can implement an interface and each class is free to provide their own way of implementation. That’s how java uses interfaces, Java fully utilizes “one interface, multiple methods” aspect of polymorphism.
  • There are mainly two types of polymorphism in Java, compile time or static polymorphism and run time or Dynamic polymorphism.
  • Method overloading is an example of compile time or static polymorphism in Java.
  • Method overriding is an example of run time polymorphism in Java.

Would you like to watch on YouTube:

That’s all about Understating of Polymorphism in Java
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

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