Introduction
Java is a powerful object-oriented programming language that supports key OOP concepts such as inheritance, encapsulation, abstraction, and polymorphism.
Among these, polymorphism stands out as one of the most important features that makes Java flexible and extensible. It allows objects to behave differently based on their data type or class, even when they share the same interface.
What is Polymorphism in Java?
The word polymorphism comes from Greek, meaning “many forms.”
In Java, polymorphism allows one interface to be used for a general class of actions. It enables you to perform a single action in different ways.
In simple terms, polymorphism means one name, multiple forms.
Types of Polymorphism in Java
Java supports two types of polymorphism:
-
Compile-Time Polymorphism (Static Binding)
-
Runtime Polymorphism (Dynamic Binding)
Let’s understand each one with examples.
1. Compile-Time Polymorphism (Method Overloading)
Compile-time polymorphism occurs when multiple methods in the same class have the same name but different parameters. This is known as method overloading.
Example:
class MathOperation {
// Method with one parameter
int add(int a, int b) {
return a + b;
}
// Overloaded method with different parameters
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperation obj = new MathOperation();
System.out.println(obj.add(10, 20)); // calls int version
System.out.println(obj.add(10.5, 20.3)); // calls double version
}
}
Output:
30
30.8
In this example, the method add() is overloaded — Java decides which version to call at compile time based on the arguments.
2. Runtime Polymorphism (Method Overriding)
Runtime polymorphism occurs when a subclass provides a specific implementation of a method that is already defined in its parent class.
This is achieved through method overriding.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a; // reference of parent class
a = new Dog();
a.sound(); // Dog’s version executes
a = new Cat();
a.sound(); // Cat’s version executes
}
}
Output:
Dog barks
Cat meows
Here, the reference variable of the parent class Animal is used to refer to objects of different child classes.
The method that gets executed depends on the object being referred to, not the reference type — this is runtime polymorphism.
Why Use Polymorphism?
Polymorphism provides several advantages in Java programming:
-
✅ Code Reusability: You can write generic code that works for multiple object types.
-
✅ Flexibility and Maintainability: It’s easy to extend or modify programs.
-
✅ Improved Readability: Makes code cleaner and easier to understand.
-
✅ Supports Loose Coupling: Classes are less dependent on each other.
Real-Life Example of Polymorphism
Think of a remote control that can operate different devices — TV, AC, or Music System — depending on what it’s connected to.
Similarly, in Java, a single interface can represent multiple classes, and each class can behave differently.
Conclusion
Polymorphism is one of the most powerful features of Java’s object-oriented design. It allows developers to write flexible, maintainable, and extensible code by enabling one interface to represent multiple behaviors.
Understanding polymorphism is essential for mastering Java and building scalable object-oriented applications.
Comments
Post a Comment