Polymorphism is one of the four core concepts of Object-Oriented Programming (OOP) in Java, alongside encapsulation, inheritance, and abstraction. At its core, polymorphism allows objects to take on multiple forms, enabling flexible and scalable code.
π What is Polymorphism in Java?
Polymorphism in Java enables a single interface or method to operate on different types of data. It makes code extensible, easier to maintain, and allows you to write generalized code that works across different object types.
Java supports two types of polymorphism:
- Compile-Time Polymorphism (Static Binding)
- Runtime Polymorphism (Dynamic Binding)
β
1. Compile-Time Polymorphism (Method Overloading)
Definition:
This occurs when multiple methods in the same class have the same name but different parameter lists (type, number, or order).
Key Characteristics:
- Happens at compile time.
- Also called method overloading.
- Does not require inheritance.
Example:
java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}
In this example, the add
method is overloaded three times to handle different input types and counts.
β
2. Runtime Polymorphism (Method Overriding)
Definition:
Occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
Key Characteristics:
- Happens at runtime.
- Requires inheritance.
- Uses method overriding.
- Enables dynamic method dispatch (calling methods based on the object's actual type at runtime).
Example:
java
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat meows");
}
}
java
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog(); // Polymorphism in action
Animal myCat = new Cat();
myDog.makeSound(); // Output: Dog barks
myCat.makeSound(); // Output: Cat meows
}
}
Here, makeSound()
behaves differently depending on the actual object type (Dog
, Cat
), even though it's called on a reference of type Animal
.
π Comparison Table: Overloading vs Overriding
FeatureMethod OverloadingMethod OverridingOccurs AtCompile-timeRuntimeClass RelationshipSame classSubclass & Superclass (Inheritance)Method SignatureMust differMust be the sameAccess ModifiersNo restrictionCan't reduce visibilityReturn TypeCan differShould be the same or covariantKeyword UsedNone@Override
annotation (optional)
π§ Why Use Polymorphism?
- Promotes code reusability and flexibility
- Enables loose coupling between components
- Makes code easier to extend and maintain
- Supports the Open/Closed Principle (Open for extension, closed for modification)
π Conclusion
Polymorphism in Java is a powerful OOP feature that allows objects to exhibit different behaviors depending on their type. Mastering both compile-time and runtime polymorphism is key to writing clean, extensible, and robust Java applications.
Whether youβre designing a flexible API, implementing a plugin system, or simply organizing your codebase better β polymorphism makes your Java code smarter and future-proof.