What is Polymorphism in Java and How to Implement It?

Polymorphism in Java is one of the critical concepts you need to learn, to understand the Object-Oriented Programming Paradigm. Polymorphism in Java is the phenomenon by which an object can acquire an ability to operate from different perspectives.

We will learn Polymorphism in Java through the following docket: 

  • What is Polymorphism?
  • Characteristics/Features of Polymorphism
  • Types of Polymorphism
  • Compile-Time Polymorphism v/s Run-Time Polymorphism
  • Super Keyword
  • Advantages of Polymorphism
  • Disadvantages of Polymorphism

What is Polymorphism?

Polymorphism_in_Java.p

According to chemistry, the term polymorphism means that an object can exist in different crystalline forms. For example, carbon can exist in three common types. Coal, graphite, and diamond are the three different crystalline forms of carbon.

Similarly, in Java, Polymorphism is a phenomenon of an object that can exhibit a property of performing mathematical and logical operations from different perspectives.

Example:

package polymorphism;

class AnimalSounds {

public void Sound() {

System.out.println("The animals make different sounds when asked to speak. For example:");

}

}

class Cow extends AnimalSounds {

public void Sound() {

System.out.println("The cow says: moh moh");

}

}

class cat extends AnimalSounds {

public void Sound() {

System.out.println("The cat says: mew mew");

}

}

class Dog extends AnimalSounds {

public void Sound() {

System.out.println("The dog says: bow wow");

}

}

public class AnimalMain {

public static void main(String[] args) {

AnimalSounds Animal = new AnimalSounds();

AnimalSounds cow = new Cow();

AnimalSounds cat = new cat();

AnimalSounds Dog = new Dog();

Animal.Sound();

cow.Sound();

cow.Sound();

Dog.Sound();

}

}

//Output:

The animals make different sounds when asked to speak. For example:

The cow says: moh moh

The cow says: moh moh

The dog says: bow wow

Now that we have a better understanding of Polymorphism in Java, let us move ahead into the characteristics of Polymorphism in Java.

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

Characteristics/Features of Polymorphism

Following are the significant characteristics of Polymorphism in Java:

  • The functionality of a method behaves differently in different scenarios.
  • The behavior of a method depends on the data provided.
  • It allows the same name for a member or method in a class with different types.
  • Polymorphism supports implicit type conversion.

Next, we will learn about the different types of Polymorphism in Java.

Types of Polymorphism

There are two different types of Polymorphism in Java. They are:

  • Compile-Time Polymorphism
  • Run-Time Polymorphism

Compile-Time Polymorphism

A typical Java program encounters Compile-Time Polymorphism during the compilation stage. Here, the overloading method resolution takes place in the compilation stage. There are two occurrences of Compile-Time Polymorphism, which are:

Method Overloading

Method Overloading is the process in which the class has two or more methods with the same name. Nevertheless, the implementation of a specific method occurs according to the number of parameters in the method call. 

Example:

//Method Overloading.

package polymorphism;

public class Addition {

public int add(int x, int y) {

return (x + y);

}

public double add(double d, double e, double f, double g) {

return (d + e + f + g);

}

public double add(double a, double b) {

return (a + b);

}

public static void main(String args[]) {

Addition a = new Addition();

System.out.println(a.add(25, 30));

System.out.println(a.add(10.0, 15.0, 20.0, 25.0));

System.out.println(a.add(127.5, 123.5));

}

}

//Output:

5555

70.070.0

251.0251.0

Operator Overloading

Method Overloading is a process where a class has two or more methods with the same name. Still, the implementation of the specific method takes place according to the number of parameters in the method definition.

Java does not support Operator Overloading to avoid ambiguities.

Run-Time Polymorphism

Run-Time Polymorphism is a procedure where the program execution takes place during Run-Time. Here, the resolution of an overriding happens in the execution stage. There are two occurrences of Run-Time Polymorphism.

Method Overriding

Method Overriding is a procedure in which the compiler can allow a child class to implement a specific method already provided in the parent class.

Example:

//Method Overriding.

package polymorphism;

class CargoPilot {

public void FlyPlane() {

System.out.println("This is the Cargo Pilot, Ready to Take off");

}

}

class CivilianPilot extends CargoPilot {

public void FlyPlane() {

System.out.println("This is the Civilian Pilot, Ready to Takeoff");

}

}

public class Takeoff {

public static void main(String args[]) {

CargoPilot CPObj = new CargoPilot();

CPObj.FlyPlane();

//CivilianPilot CivilianObj = new CivilianPilot();

//CivilianObj.FlyPlane();

}

}

//Output:

This is the Cargo Pilot, Ready to Takeoff

Operator Overriding

Operator Overriding a procedure where you can define an operator in both parent and child classes with the same signature, but with different operational capability.

Java does not allow Operator Overriding to avoid ambiguities.

Let’s draw up the differences between Compile-Time and Run-Time Polymorphism to get a better understanding.

Compile-Time Polymorphism vs. Run-Time Polymorphism

Compile-Time Polymorphism

Run-Time Polymorphism

  • The method call is handled by the compiler
  • The compiler cannot control the method call in run-time
  • Compile-Time Polymorphism is less flexible, as it needs to handle all method calls in compile-time
  • Run-Time Polymorphism exhibits higher flexibility as the method calls get handled at run-time
  • The execution period for the Compile-Time Polymorphism is less
  • The execution period for the Run-Time Polymorphism is more
  • Integrating the right method call with the proper method is done in compile-time
  • Combining the correct method call with the right method is done in run-time
  • Occurs during Method Overloading and Operator Overloading
  • Occurs during Method Overriding

With this, we have understood the differences between the types of Polymorphism in Java and the working terminology of Run-Time Polymorphism and Compile-Time Polymorphism. 

Since the process of polymorphism deals with extending the methods and members of the parent class, we need to know how to extend the members and methods, particularly from the parent class.

In the next part, we will learn about a reference keyword that is used to refer to the parent class objects. Unlike “this” keyword, the “super” keyword is what we will be exploring.

Super Keyword

The term "super" is a keyword in Java that refers to the program's immediate parent class object or method. In this procedure, whenever you create an instance of a subclass, automatically, the compiler will create an instance of the parent class implicitly. The super reference variable will be referring to the parent class’ instance.

Example:

//Super Keyword

package polymorphism;

public class SuperKeyWord {

public static void main(String[] args) {

triangle two = new triangle();

two.countsides();

}

}

class square {

int sides = 4;

}

class triangle extends square {

int sides = 3;

public void countsides() {

System.out.println("Number of sides in the square : " + sides);

System.out.println("Number of sides in the triangle : " + super.sides);

}

}

//Output:

Number of sides in the square: 3

Number of sides in the triangle: 4

Advantages of Polymorphism

  • Programmers code can be reused via Polymorphism
  • Supports a single variable name for multiple data types
  • Reduces coupling between different functionalities
Do you wish to become a Java Developer? Check out the Java Certification Training Course and get certified today.

Disadvantages of Polymorphism

  • Polymorphism ends up raising performance issues in real-time
  • Polymorphism reduces the readability of the code
  • Programmers find Polymorphism a little challenging to implement

So, these are a few important advantages and disadvantages of Polymorphism in Java.

With this, we have arrived at the end of this "Polymorphism in Java" article. We hope you enjoyed understanding the essential concepts of Polymorphism in Java.

Are you interested in Java Programming Language and getting certified as a professional Java Developer? Then, check out our Java training and certification program. It is curated by the most experienced real-time industry experts.

Got a question on "Polymorphism in Java"? Please mention it in the article's comment section, and we'll have our experts answer it for you right away.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.