Polymorphism in Java

The literal meaning of Polymorphism is having different forms. This principle can be applied in Java or any other Object Oriented language.

There are two forms of Polymorphism:

  • Method Overloading
  • Method Overriding

Method Overloading –

In a class when two methods are created with the same name but different signature where the signature can differ by the number of arguments or by the different type of arguments or by both.

Method Overloading is a compile-time polymorphism as at the compile time it is decided which method will be called.

Let us consider an example:

Let us say we have a class called Shape which calculates the area of different shapes. Consider three shapes – square, rectangle, and circle.

Area of Square: side * side;

Area of Rectangle: length * width;

Area of Circle: PI * radius * radius;

This shape class will have three methods with the same name say calculateArea().

  1. For calculating the area of the square where only one argument of type int will be passed.
  2. For calculating the area of the rectangle where two arguments both of type int will be passed.
  3. For calculating the area of circle where one argument of type double is passed.

See below code for better understanding:

package polymorphism;

public class Shape {
	
	public double calculateArea(int side){
		
		return side * side;
	}
	
	public double calculateArea(int length, int width){
		
		return length * width;
	}
	
	public double calculateArea(double radius){
		
		return radius * radius;
	}

}

With the help of Method Overloading, you don’t have to remember the names of different methods you will create to perform the same type of functionality.

Method Overriding –

When methods with same name and same signature are implemented in both superclass and subclass is known as Method Overriding. Here, the implementation written in subclass overrides the implementation of the superclass.

Sometimes, there is a requirement that you can use some of the existing methods of a class, but a few require some changes. You can override all those methods which require changes.

Let us consider an example:

we have a class Machine with a run method with some implementation, and we want different implementation in Car class which extends this Machine class.

package polymorphism;

public class Machine {
	
	public void run(){
		System.out.println("Machine is running");
	}

}
package polymorphism;

public class Car extends Machine {
	
	@Override
	public void run(){
		System.out.println("Car is running");
	}

}
package polymorphism;

public class DemoCar {
	
	public static void main(String[] args) {
		
		Car alto = new Car();
		alto.run();
		
		Machine bmw = new Car();
		bmw.run();
	}

}

When this code is executed, for both run calls – “Car is running” will be printed.

This also states that Method overriding is a runtime polymorphism because it is decided at runtime which method to execute.

Some points to remember for Method Overriding:

  • Methods must have the same name.
  • Methods must have same arguments.
  • Static Methods cannot be overridden because static methods belong to class not to object.
  • We cannot override the main method because it is a static method.
  • If you override a method, the overridden method must not be more restricted. Example – a protected method cannot be overridden by a default method.

PS: For any questions, queries or comments feel free to write us at support@qatechub.com or saurabh@qatechhub.com

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: