Polymorphism, meaning "many forms," is a core concept in object-oriented programming (OOP). It allows objects of different classes to be treated as objects of a common superclass. Here are key examples:
1. Method Overriding 🔄
When a subclass provides a specific implementation of a method already defined in its superclass.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark!");
}
}
👉 Explore OOP Concepts in Depth
2. Method Overloading 📌
Multiple methods with the same name but different parameters in the same class.
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
3. Interface Implementation 🧩
Objects can take multiple forms by implementing interfaces.
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing a circle");
}
}
Visualizing Polymorphism
For further learning, check our Java OOP Fundamentals guide. 🚀