Monday , October 23 2017
Home / java / OOPS / Types of Polymorphism and advantages

Types of Polymorphism and advantages

Polymorphism is a significant OOP feature in Java. It enables programmers to create code that is easier to understand and reuse. Polymorphism is a powerful feature because it works together with the other OOP principles to create scalable programs.

The OOP principles that form the core of Java programming are abstraction, encapsulation, inheritance, and polymorphism.

What is Polymorphism ?

The term polymorphism means many forms. In Java, polymorphism enables you to define a single interface for accessing multiple related behaviors. By using the polymorphism features in Java, you can create multiple methods that have the same name but provide different functionalities. The exact method to be executed is determined either by the compiler or the JVM depending on the type of polymorphism used.

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. In 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());
    }
}
Output:
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 …

Leave a Reply

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