Java Polymorphism Concepts – Complete Guide with Examples
Learn the concepts of polymorphism in Java, including compile-time and runtime polymorphism, with detailed examples to implement flexible and maintainable object-oriented programs.
Polymorphism Concepts in Java – Complete Detailed Tutorial
Polymorphism is a core concept of Object-Oriented Programming (OOP) that means "many forms".
It allows objects to take multiple forms and enables flexible code and method overriding/overloading.
1. Types of Polymorphism in Java
Java supports two types of polymorphism:
- Compile-time Polymorphism (Static Polymorphism)
- Runtime Polymorphism (Dynamic Polymorphism)
2. Compile-time Polymorphism (Method Overloading)
- Occurs when the method to be called is determined at compile time
- Achieved using method overloading and operator overloading (not in Java)
Example – Compile-time Polymorphism:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // int version
System.out.println(calc.add(5.5, 6.5)); // double version
System.out.println(calc.add(1, 2, 3)); // 3-int version
}
}
Output:
30
12.0
6
Explanation:
- The compiler determines which
addmethod to call based on parameters
3. Runtime Polymorphism (Method Overriding)
- Occurs when the method to be called is determined at runtime
- Achieved using inheritance and method overriding
- Uses dynamic method dispatch
Example – Runtime Polymorphism:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a1 = new Dog(); // parent reference to Dog
Animal a2 = new Cat(); // parent reference to Cat
a1.sound(); // Dog's method called at runtime
a2.sound(); // Cat's method called at runtime
}
}
Output:
Dog barks
Cat meows
Explanation:
- The object type at runtime decides which method to call
4. Polymorphism with Parameters (Polymorphic Arguments)
- Methods can accept parent class references, and child objects can be passed
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
class Zoo {
void makeSound(Animal a) { // polymorphic parameter
a.sound();
}
}
public class Main {
public static void main(String[] args) {
Zoo zoo = new Zoo();
zoo.makeSound(new Dog());
zoo.makeSound(new Cat());
}
}
Output:
Dog barks
Cat meows
5. Key Points of Polymorphism
- Polymorphism = Many Forms
- Compile-time Polymorphism: Method overloading, operator overloading
- Runtime Polymorphism: Method overriding, dynamic method dispatch
- Enables flexible, reusable, and maintainable code
- Achieved using inheritance, interfaces, and overriding