Tech Master Tutorials
Email Facebook Google LinkedIn Pinterest Twitter
Home Java Java 8 Java Interview Questions Java8 Interview Questions Object Oriented Programming in Java JVM Java Programming

Polymorphism

Polymorphism is having the same name but multiple forms.
In java we have static and dynamic polymorphism.

Static Polymorphism(Method Overloading) :
Have a look at the below OverloadingTest class, here we have two methods with the same name but different parameters. When we can the method with one parameter then overloadedMethod(String param) is called and when we call the same method with two parameters, the other overloadedMethod with two parameters is called.

package oops.polymorphism;

public class OverloadingTest {
	
	public void overloadedMethod(String param){
		System.out.println("inside overloadedMethod, parama = "+ param);
	}
	
	public void overloadedMethod(String param1, String param2){
		System.out.println("inside overloadedMethod, parama1 = "+ param1 +" and param2 = "+ param2);
	}
	
	public static void main(String[] args) {
		OverloadingTest obj = new OverloadingTest();
		
		//calling method with one parameter
		obj.overloadedMethod("test parameter");
		
		//calling method with two parameters
		obj.overloadedMethod("test parameter1", "test parameter2");
	}

}
Lets have a look at the below example of a class where we are calculating the area of a circle and a rectangle as well. Here we have two different methods: calculateCircleArea() : method to calculate the area of a circle calculateRectangleArea() : method to calculate the area of a rectangle
package oops.polymorphism;

public class AreaTest {
	
	//method to calculate the area of a circle, r is the radius
	public double calculateCircleArea(double r){
		return Math.PI*r*r;
	}
	
	//method to calculate the area of a rectangle, rectangle has sides m and n
	public double calculateRectangleArea(double m, double n){
		return m*n;	
	}
	
	
	public static void main(String[] args) {
		AreaTest obj = new AreaTest();
		
		double areaOfCircle = obj.calculateCircleArea(3);
		System.out.println("area of circle = "+ areaOfCircle);
		
		double areaOfRectangle = obj.calculateRectangleArea(5, 4);
		System.out.println("area of rectangle = "+ areaOfRectangle);
		
		
	}
	

}
Here we can apply method overloading, we can have the same method calculateArea() for both with different parameters like below :
package oops.polymorphism;

public class AreaOverloadingTest {
	
	//method to calculate the area of a circle, r is the radius
	public double calculateArea(double r){
		return Math.PI*r*r;
	}
	
	//method to calculate the area of a rectangle, rectangle has sides m and n
	public double calculateArea(double m, double n){
		return m*n;	
	}
	
	
	public static void main(String[] args) {
		AreaOverloadingTest obj = new AreaOverloadingTest();
		
		double areaOfCircle = obj.calculateArea(3);
		System.out.println("area of circle = "+ areaOfCircle);
		
		double areaOfRectangle = obj.calculateArea(5, 4);
		System.out.println("area of rectangle = "+ areaOfRectangle);
		
		
	}
	

}

Since the method is decided at compile time by compiler, which method to call on the basis of the parameters passed. That’s why method poverloading is called static polymorphism.


Dynamic Polymorphism(Method Overriding) : Overriding is when a child class overrides the parent class method.
In case of overriding there are two classes involved, when parent class and child class both have the method with the same name and same signature, but when call the method with the child class object reference, child class method is called.
Lets have a look at the below example

Lets have a look at the below lines of code where we have Rectangle and Square classes. Square is the child class of Rectangle class and we are overriding display() method of Rectangle in Square class.
package oops.polymorphism;

public class Rectangle{
	
	public void display(){
		System.out.println("Rectangle class");
	}

}

package oops.polymorphism;

public class Square extends Rectangle {
	
	@Override
	public void display(){
		System.out.println("Square class");
	}
	
	public static void main(String[] args) {
		Rectangle rectangle = new Square();
		rectangle.display();
	}
	
}

Output upon executing the main method of Square class:
Square class

So here after executing the main method, display method in the square class is being called.
This is method overriding .
Also this is Runtime polymorphism as at compile time Rectangle class reference knows only the method from Rectangle class
and only at run time it is known that Square class instance is being referred, and jvm determines at runtime that Square
class display method is to be called.
A reference can be re used to refer objects of different classes(child classes) at different places in code.
If a parent reference is holding the reference of child class then overridden methods of child classes will be called.