In general, the word polymorphism means multiple forms. In Java, polymorphism means using a particular element in multiple ways. For instance, Let’s assume that we have a base class “furniture” and it has two subclasses “desk” and “wardrobe” and there is a method “material” in the base class.
 

Now, in both the subclasses, the “material” method will be used, and with polymorphism, it is possible to use the same method “material” of parent class in both the subclass.

In Java, there are two types of Polymorphism available.

  • Static Polymorphism
  • Dynamic Polymorphism

In this tutorial, you’ll learn about both of these with appropriate illustrations.

Static Polymorphism in Java

Static Polymorphism is detected by the compiler in compile-time and all the steps to perform this polymorphism are prepared by the compiler and before proceeding with the run-time process, the compiler prepares the system.

Method overloading and Operator overloading are two ways to achieve static polymorphism in the program.

For instance:

class Adddemo {

 // Addition Method with 2 parameters
 static int Addition(int x, int y) {
  return x + y;
 }

 //Another Addition Method with the same name but it has 3 parameters
 static int Addition(int x, int y, int z) {
  return x + y + z;
 }
}

class Main {
 public static void main(String[] args) {
  Adddemo ob = new Adddemo();
  ob.Addition(5, 9));
 ob.Addition(5, 7, 2));
}
}

Output:

14
14

Dynamic Polymorphism in Java

Dynamic polymorphism occurs at the run time and JVM handles all the steps required to make it happen. All the required decisions are taken by JVM and the compiler is unaware of this polymorphism.

Method overriding is a way to achieve dynamic polymorphism. In dynamic polymorphism, we need two classes (parent and base both).

For instance:

class Parentclass {

 void display() {
  System.out.println("This is the parent class");
 }
}

class childclass extends Parentclass {

 void display() {
  System.out.println("This is Child class");
 }
}

class secondchildclass extends Parentclass {

 void display() {
  System.out.println("This is secondsubclass");
 }
}

class Mainclass {
 public static void main(String[] args) {

  Parentclass ob;
  ob = new childclass();
  ob.display();
  ob = new secondchildclass();
  ob.display();
 }
}

In the above program, if you notice, the display method is used in both the child classes and parent class. But each of these has a different body. With the ob instance of the parent class, the display method of child classes was called.

This is what polymorphism means, using the same method or element in multiple ways.

Advantages of Polymorphism in Java

  • Reduces Line of Code
  • Makes debugging easy
  • Execution time is reduced
  • Code reusability is increased
  • Programs become more readable
  • Space complexity is reduced

Conclusion

For a beginner, polymorphism is a bit tricky to understand but after a few practice programs, you’ll find it extremely helpful while implementing complex programs with different interrelated classes. Polymorphism plays a very important role in interlinked class and it makes the whole handling of data easy for the programmer, besides this for the readers it becomes easy to read and understand.

Polymorphism might not seem important right now but when you’ll implement tricky programs, you will find it extremely efficient.

Last modified: July 16, 2020

Author

Comments

Write a Reply or Comment

Your email address will not be published.