Monday , January 22 2018
Home / java / OOPS / Types of Polymorphism and advantages

Types of Polymorphism and advantages

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

What is Polymorphism ?

Polymorphism is a significant feature of Object Oriented Principles. The word polymorphism came from two greek words ‘poly‘ and ‘morphos‘. Here poly means many and morphos 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 a 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.

Polymorphism Example :

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);
    }

}

On the above example Car instance has assigned with Maruti and Tata objects respectively. But by calling with Maruti object reference it will refer to Maruti class speed() method and with Tata object reference it will refer to Tata class speed() method.

Maruti Speed is :200
Tata Speed is :300

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");
    }
}

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.

Example:

public class Parent {

    /**
     * Overridden Method
     */
    public void method() {
        System.out.println("Parent Method.");
    }

    /**
     * Overridden Method
     */
    public Object method2() {
        return "Parent Method2.";
    }
}

class Child extends Parent {

    /**
     * Method Overriding
     */
    public void method() {
        System.out.println("Child Method");
    }

    /**
     * Method Overriding
     */
    public String method2() {
        return "Child Method2.";
    }
}

public class OverridingDemo {

    public static void main(String[] args) {
        Child child = new Child();
        child.method();
        Parent parent = new Child();
        System.out.println("parent.method2() : " + parent.method2());
    }
}

Child Method
parent.method2() : Child Method2.

Method overriding occurs in a program when a subclass includes a method with the same name and type signature as its super class method. Whenever the overridden method is invoked from the subclass, Java executes a call to the subclass method. In this way, the subclass method overrides the super class method and keeps it hidden.

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 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 chandrashekhar

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+ ,

Recommended

Difference between Aggregation and Composition

Aggregation: Aggregation is one of the special case of Association. The directional association present in …

One comment

  1. sravanthi.gowlikar@wipro.com'

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *