Java Polymorphism

Before jumping to know about Polymorphism? First, Let’s understand what is the scenario before Polymorphism?

Suppose, If we want to find the area of different shapes like Area of Square, Area of Polygon or Area of the triangle, for this all we need to create different methods for every single shape, Isn’t it make our code repetitive? Or Incompatible? Of Course Yes, It will.

To fix this problem Polymorphism came into the picture with a code reusability feature which helps to save a lot of time.

What is Polymorphism?

Polymorphism is a feature of the object-oriented programming language, Java, which implies that you can perform a single task in different ways. It simply means more than one form.

Real-Life Example:

A woman can be a mother, a sister, a daughter, a friend, all at the same time, i.e. she performs other behavior according to the situation.

The human body has different organs. Every organ has a different function to perform; the heart is responsible for blood flow, the eyes for reading, the brain for cognitive activity, and the hands are for writing. So we have a standard method function that performs differently depending upon the organ of the body.

Advantages of Polymorphism:

  • Easy to debug the code, which saves a lot of time.
  • Providing reusability to the code, we can use the same name method multiple times with slight variations.

Types of Polymorphism : 

  1. Compile-time Polymorphism
  2. Runtime Polymorphism

Compile Time Polymorphism :

  • It is also called Static Polymorphism
  • It can be achieved by Method Overloading and Operator Overloading.
  • However, Java does not support Operator Overloading.

Method Overloading is when a class has multiple methods with the same name, but the number, types, and order of parameters and the return type of the methods are different.

Example of compile-time Polymorphism:

Java Code

// example of compile-time polymorphism

public class Addition {

  void sum(int a, int b) //same method name with different parameters
  {
    int c = a + b;
    System.out.println(c);
  }
  void sum(int a, int b, int e) // same method name with different parameters
  {
    int c = a + b + e;
    System.out.println(c);
  }
}
//main class
class TUF {
  public static void main(String[] args) {
    // Creating object of class 1
    Addition obj = new Addition();
    obj.sum(30, 90);
    obj.sum(45, 80, 22);
  }
}

Output:  

8

34.65

In this program, the sum() method overloads two types via different parameters. 

Runtime Polymorphism:

  • Runtime polymorphism in Java is also popularly known as Dynamic Binding or Dynamic Method Dispatch.
  •  You can achieve Runtime polymorphism via Method Overriding.

Method Overriding:

Method Overriding is done when a child or a subclass has a method with the same name, parameters, and return type as the parent or the superclass; then that function overrides the function in the superclass.

Example of Run-time Polymorphism:

Java Code

// Java Program for Method Overriding

class Parent {

  void Display() {
    System.out.println("parent class");
  }
}

class childclass1 extends Parent {
  void Display() {
    System.out.println("childclass1");

  }
}

class childclass2 extends Parent {

  void Display() {
    System.out.println("childclass2");
  }
}

class TUF {
  public static void main(String[] args) {
    Parent a;

    a = new childclass1();
    a.Display();

    a = new childclass2();
    a.Display();
  }
}

Output:

childclass1

childclass2

In the above program parent class method is overridden by the child class, that’s why the child class has the priority now to execute.

Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article