Saturday, January 9, 2021

Polymorphism in Java

Polymorphism is one of the four fundamental OOPS concepts. The other three are Inheritance, Encapsulation, Abstraction. In this post we'll see examples of Polymorphism in Java.


Polymorphism Concept

Polymorphism, 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 is often expressed as "One interface, multiple methods". Where one general class may have the generic method and the derived classes (classes which extend the general class) may add class specific implementation to that method. At the time of execution "One interface" i.e. the general class will take "many forms" i.e. references of the derived classes (See example to have clarity).

Note that Polymorphism is extensively used with inheritance where parent class methods are overridden by the child classes.

Polymorphism in Java

There are 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, which of the overridden method would be called, is resolved at run time. The decision is based on the object reference the reference variable is pointing to.

Run time polymorphism Java example

Let's see an example of run time polymorphism in Java to understand the concept clearly. 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.

// Super Class
class Shape{
 protected double length;
 Shape(double length){
  this.length = length;
 }
 void area(){
 }
}

// Child class
class Square extends Shape{
 //constructor to initialize length
 Square(double side){
  super(side); // calling the super class constructor  
 }
 //Overriding the area() method
 void area(){
  System.out.println("In area method of square");
  System.out.println("Area of square - " + length*length);
 }
}

// Child class
class Circle extends Shape{
 //constructor to initialize length
 Circle(double radius){
  super(radius); // calling the super class constructor 
 }
 //Overriding the area() method
 void area(){
  System.out.println("In area method of circle");
  System.out.println("Area of cirlce - " + 22/7*length*length);;
 }
}

public class PolymorphicTest {
 public static void main(String[] args){
  Shape shape;
  Square square = new Square(5.0); 
  Circle circle = new Circle(5.0); 
  // shape dynamically bound to the Square object referenced by square
  shape = square;
  // area method of the square called
  shape.area(); 
  // shape dynamically bound to the Circle object referenced by circle
  shape = circle; 
  // area method of the circle called
  shape.area();   
 }
}

Notice that in PolymorphicTest 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-

In area method of square
Area of square - 25.0
In area method of circle
Area of cirlce - 75.0

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.

Compile time polymorphism Java example

In the Java example for compile time polymorphism there are two overloaded methods, one having a single int parameter where as other method has one int and one String parameter. Which of the overloaded method has to be called is decided based on the parameters passed and this decision happens at the time of compilation itself.

public class OverloadingExample {
 // overloaded Method
 void overloadedMethod(int i){
  System.out.println("In overloaded Method with int parameter- " + i);
 }
 
 // overloaded Method
 void overloadedMethod(int i, String s){
  System.out.println("In overloaded Method with int and string parameters- " + i + " " + s);
 }
 
 public static void main(String args[]){ 
  OverloadingExample obj = new OverloadingExample();
  obj.overloadedMethod(5);
  obj.overloadedMethod(5, "Test");
 }
}

Output

In overloaded Method with int parameter- 5
In overloaded Method with int and string parameters- 5 Test

Points to note-

  • Polymorphism, a Greek word, where poly means many and morph means change thus it refers to the ability of an object taking many forms.
  • Any number of classes can implement an interface and each class is free to provide their own implementation. That's how using interfaces, Java fully utilizes "one interface, multiple methods" aspect of polymorphism.
  • There are two types of Polymorphism in Java, compile time polymorphism and run time polymorphism.
  • Method overloading is an example of compile time polymorphism in Java.
  • Method overriding is an example of run time polymorphism in Java.

That's all for this topic Polymorphism in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Method Overloading in Java
  2. Method Overriding in Java
  3. Interface in Java With Examples
  4. Java Abstract Class and Abstract Method
  5. Java OOP Interview Questions And Answers

You may also like-

  1. Object in Java
  2. Marker Interface in Java
  3. Difference Between Abstract Class And Interface in Java
  4. Why Class Name And File Name Should be Same in Java
  5. Java Exception Handling And Method Overriding
  6. @FunctionalInterface Annotation in Java
  7. Count Number of Words in a String Java Program
  8. Race Condition in Java Multi-Threading