Open In App

Polymorphism in Java

Last Updated : 08 Jul, 2025
Comments
Improve
Suggest changes
587 Likes
Like
Report

Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity can take many forms. In Java, polymorphism allows the same method or object to behave differently based on the context, specially on the project's actual runtime class.

Key features of polymorphism:

  • Multiple Behaviors: The same method can behave differently depending on the object that calls this method.
  • Method Overriding: A child class can redefine a method of its parent class.
  • Method Overloading: We can define multiple methods with the same name but different parameters.
  • Runtime Decision: At runtime, Java determines which method to call depending on the object's actual class.

Real-Life Illustration of Polymorphism

Consider a person who plays different roles in life, like a father, a husband, and an employee. Each of these roles defines different behaviors of the person depending on the object calling it.

Example: Different Roles of a Person


Output
I am a father.

Explanation: In the above example, the Person class has a method role() that prints a general message. The Father class overrides role() to print a specific message. The reference of type Person is used to point to an object of type Father, demonstrating polymorphism at runtime. The overridden method in Father is invoked when role() is called.

Why Use Polymorphism In Java?

Using polymorphism in Java has many benefits which are listed below:

  • Code Reusability: Polymorphism allows the same method or class to be used with different types of objects, which makes the code more useable.
  • Flexibility: Polymorphism enables object of different classes to be treated as objects of a common superclass, which provides flexibility in method execution and object interaction.
  • Abstraction: It allows the use of abstract classes or interfaces, enabling you to work with general types (like a superclass or interface) instead of concrete types (like specific subclasses), thus simplifying the interaction with objects.
  • Dynamic Behavior: With polymorphism, Java can select the appropriate method to call at runtime, giving the program dynamic behavior based on the actual object type rather than the reference type, which enhances flexibility.

Types of Polymorphism in Java

In Java Polymorphism is mainly divided into two types: 

  1. Compile-Time Polymorphism (Static)
  2. Runtime Polymorphism (Dynamic)
poly

1. Compile-Time Polymorphism

Compile-Time Polymorphism in Java is also known as static polymorphism and also known as method overloading. This happens when multiple methods in the same class have the same name but different parameters.

Note: But Java doesn't support the Operator Overloading.

Java Polymorphism

Method Overloading

As we discussed above, Method overloading in Java means when there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments.

Example: Method overloading by changing the number of arguments


Output
8
34.65

Explanation: The Multiply method is overloaded with different parameter types. The compiler picks the correct method during compile time based on the arguments.

2. Runtime Polymorphism

Runtime Polymorphism in Java known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

Method Overriding

Method overriding in Java means when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass. Method overriding allows a subclass to modify or extend the behavior of an existing method in the parent class. This enables dynamic method dispatch, where the method that gets executed is determined at runtime based on the object's actual type.

Example: This program demonstrates method overriding in Java, where the Print() method is redefined in the subclasses (subclass1 and subclass2) to provide specific implementations.


Output
subclass1
subclass2

Explanation: In the above example, when an object of a child class is created, then the method inside the child class is called. This is because the method in the parent class is overridden by the child class. This method has more priority than the parent method inside the child class. So, the body inside the child class is executed.

Advantages of Polymorphism

  • Encourages code reuse.
  • Simplifies maintenance.
  • Enables dynamic method dispatch.
  • Helps in writing generic code that works with many types.

Disadvantages of Polymorphism

  • It can make more difficult to understand the behavior of an object.
  • This may cause performance issues, as polymorphic behavior may require additional computations at runtime.

Polymorphism with Java
Visit Course explore course icon
Next Article
Article Tags :
Practice Tags :

Similar Reads