Types of Polymorphism and advantages

By | 2018-04-12T18:16:37+00:00 June 21st, 2015|OOPS|2 Comments

In these tutorials we are going to understand what polymorphism is and what different types are.

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. And it enables the programmers to create/write programs that are easier to understand and reuse.

Polymorphism provides flexibility to the programmer to write programs that uses 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 that are,

  • compile-time polymorphism and
  • run-time polymorphism.

Compile-time Polymorphism:

The type of polymorphism that is implemented when the compiler compiles a program is called compile-time polymorphism. Java supports compile-time polymorphism through method overloading. This type of polymorphism is also called as 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 to be executed by comparing the type and number of arguments.

Example:

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:

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

Run-time Polymorphism:

The type of polymorphism that is implemented dynamically when a program being executed is called run-time polymorphism. 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.

The run-time polymorphism is also called dynamic polymorphism or late binding.

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:

Maruti Speed is :200 
Tata Speed is :300

On the above example – Maruthi and Tata are two different classes, and both were two different implementations of Car Interface. That means, these two classes are type of Car.

As these two classes are the same type, we can refer these two objects to a single type called Car like above. Here based on the type of object being asigned with the reference 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 example, 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.

  • Method overloading can be implemented on constructors allowing different ways to initialize objects of a class. This enables you to define multiple constructors for handling different types of initializations.

  • Method overriding allows a sub class to use all the general definitions that a super class 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 two methods are different whereas method overriding occurs only when the type signatures of two methods are the same.

  • In method overloading, the compiler determines the correct method to be called by comparing type signatures. In method overriding, the JVM determines the correct method to be called based on the object that the invoking variable refers to.

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

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

2 Comments

  1. sravanthi.gowlikar@wipro.com'
    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. arorapriya965@gmail.com'
    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