Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) in Java.
The word Polymorphism comes from two Greek words:
- Poly → means “many”
- Morph → means “forms”
So, polymorphism literally means “many forms.”
In simple terms, polymorphism allows the same method or object to behave differently depending on the situation.
It gives Java programs flexibility and reusability and helping developers write cleaner and more adaptable code.
Understanding Polymorphism in Java
In Java, polymorphism allows you to use a single interface or method to represent different types of behavior.
For example, when you tell different animals to make a sound, each one responds differently, but the command remains the same (makeSound()).
It means that, same method name, a different implementation is the aim of polymorphism.
Types of Polymorphism in Java
Java supports two types of polymorphism:
- Compile-Time Polymorphism (Static Binding)
- Run-Time Polymorphism (Dynamic Binding)
1) Compile-Time Polymorphism (Method Overloading)
This type of polymorphism is resolved during compilation. It happens when multiple methods in the same class have the same name but different parameters (number or type of arguments). It’s also called Method Overloading.
Example of Method Overloading:
class Calculator {
// Same method name but different parameters
void add(int a, int b) {
System.out.println("Sum: " + (a + b));
}
void add(double a, double b) {
System.out.println("Sum: " + (a + b));
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.add(5, 10); // Calls first method
calc.add(2.5, 3.5); // Calls second method
}
}
Output:
Sum: 15
Sum: 6.0
- Here, both methods are named add(), but they behave differently depending on the data type. this is compile-time polymorphism.
2) Run-Time Polymorphism (Method Overriding)
This type of polymorphism is decided during program execution. It happens when a subclass overrides a method from its parent class. It’s also called Method Overriding.
Example of Method Overriding:
class Vehicle {
void start() {
System.out.println("The vehicle is starting...");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("The car engine is starting...");
}
}
class Bike extends Vehicle {
@Override
void start() {
System.out.println("The bike engine is starting...");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v1 = new Car(); // reference of parent, object of child
Vehicle v2 = new Bike();
v1.start(); // Executes Car’s start()
v2.start(); // Executes Bike’s start()
}
}
Output:
The car engine is starting...
The bike engine is starting...
Example: Polymorphic Payment System
Imagine you use a payment system where multiple payment methods exist: CreditCard, PayPal, and UPI.
Each payment type processes payments differently, but the program interacts with all of them in the same way using a Payment interface.
Example:
// Step 1: Create an interface
interface Payment {
void pay(double amount); // Abstract method
}
// Step 2: Implement the interface in different classes
class CreditCard implements Payment {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using Credit Card.");
}
}
class PayPal implements Payment {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using PayPal.");
}
}
class UPI implements Payment {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using UPI.");
}
}
// Step 3: Main class to test polymorphism
public class Main {
public static void main(String[] args) {
Payment payment;
payment = new CreditCard();
payment.pay(1500);
payment = new PayPal();
payment.pay(750);
payment = new UPI();
payment.pay(1200);
}
}
Output:
Paid ₹1500.0 using Credit Card.
Paid ₹750.0 using PayPal.
Paid ₹1200.0 using UPI.
Advantages of Polymorphism
1) Simplifies Code: Polymorphism reduces code complexity by allowing a single interface or method to work with multiple object types.
Instead of writing separate methods for each type, you can write one method that handles all objects uniformly.
2) Extensibility: Polymorphism allows new functionality to be added without modifying existing code. You can introduce a new class that implements an existing interface or extends a base class.
3) Code Reusability: With polymorphism, a single method can operate on different types of objects, promoting reusable code.