In this tutorial, we are going to understand the concept of polymorphism in Java and different types of it.

What is Polymorphism?

Polymorphism is a significant feature of Object Oriented Principles. The word polymorphism came from two Greek words ‘poly‘ and ‘morphs‘. Here poly means many and morphs means forms.

Polymorphism represents the ability of an object to assume different forms. Therefore, it enables programmers to create/write programs that are easier to understand and reuse.

Certainly, Polymorphism provides flexibility to the programmer to write programs that use a single method for different operations depending on the requirement.

 In Java, we can define a single interface to represent multiple concrete classes. So by accepting the interface type, a method can behave differently for different inputs.

Types of Polymorphism

Java supports two types of polymorphism namely:

  1. Compile-time polymorphism
  2. Run-time polymorphism.

1. Compile-time Polymorphism:

Compile-time polymorphism is the type of polymorphism occurs when the compiler compiles a program. Java supports compile-time polymorphism through method overloading. The other names of Compile-time polymorphism are static polymorphism or early binding.

Polymorphism occurs in method overloading because method overloading allows access to different methods through the same interface.

In method overloading, two or more methods in a class can use the same name as long as their parameter declarations are different.

When the compiler encounters a call to an overloaded method, it identifies the correct version of the overloaded method by comparing the type and the number of arguments.

Example:

OverloadingDemo.java
public class OverloadingDemo {
    public void meth(int value) {
        System.out.println("int value : " + value);
    }
    public void meth(String name) {
        System.out.println("String value : " + name);
    }
    public void meth(int value, String name) {
        System.out.println("Name with value : " + value+" "+name);
    }
    public static void main(String[] args) {
        OverloadingDemo demo = new OverloadingDemo();
        demo.meth(10);
        demo.meth("online tutorials point");
        demo.meth(20, "Overloading Demo");
    }
}

Output:

Console
int value : 10
String value : online tutorials point
Name with value : 20 Overloading Demo

2. Run-time Polymorphism:

Run-time Polymorphism is the type of polymorphism that occurs dynamically when a program executes. Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding.

For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time.

Since the run-time polymorphism occurs dynamically, it is also called dynamic polymorphism or late binding.

PolymorphismDemo.java
interface Car {
    void speed(int speed);
}
class Maruti implements Car {
    public void speed(int speed) {
        System.out.println("Maruti Speed is :" + speed);
    }
}
class Tata implements Car {
    public void speed(int speed) {
        System.out.println("Tata Speed is :" + speed);
    }
}
public class PolymorphismDemo {
    public static void main(String[] args) {
        Car car;
        car = new Maruti();
        car.speed(200);
        car = new Tata();
        car.speed(300);
    }
}

Output:

Console
Maruti Speed is :200 
Tata Speed is :300

In the above example – Maruthi and Tata are two different classes, and both were two different implementations of the Car Interface. In other words, these two classes are types of Car.

Since because these two classes are the same type, we can refer these two objects to a single type called Car as above. Here, based on the type of the object being assigned with the reference, polymorphism is resolved at runtime.

Usages and Advantages of Polymorphism

  • Method overloading allows methods that perform similar or closely related functions to be accessed through a common name. For instance, a program performs operations on an array of numbers which can be int, float, or double type. Method overloading allows you to define three methods with the same name and different types of parameters to handle the array of operations.

  • Constructors allowing different ways to initialize objects of a class can implement method overloading. This enables you to define multiple constructors for handling different types of initializations.

  • Method overriding allows a subclass to use all the general definitions that a superclass provides and add specialized definitions through overridden methods.

  • Method overriding works together with inheritance to enable code-reuse of existing classes without the need for re-compilation.

Differences Between Compile-time and Run-time Polymorphism

  • Method overloading occurs at Compile-time whereas method overriding occurs at Run-time.

  • Method overloading occurs when the type signatures of the two methods are different whereas method overriding occurs only when the type signatures of the two methods are the same.

  • In method overloading, the compiler calls the correct method by comparing the type signatures. However, in method overriding, the JVM determines the correct method based on the object that the invoking variable is referring to.

  • Method overloading is possible on methods with private, static, and final access modifiers. On the other hand, method overriding is not possible on these access modifiers.

References:

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

2 Comments

  1. Sravanthi November 3, 2017 at 2:38 pm - Reply

    Good info but in Advantages can be made like hitting bulls eye.
    simpe and short like code reuse,Better Performance, optimized way of achieving tasks etc.

  2. priya May 11, 2018 at 5:58 am - Reply

    The basic reason behind polymorphism is that we can implement different behaviors of the same object depending upon the reference type passed to an object.

Leave A Comment