Polymorphism
Part of Object-Oriented Programming
What is Polymorphism?
Polymorphism (from Greek, meaning "many forms") is the ability of objects to take on multiple forms. It allows the same interface to be used for different underlying data types.
In practice, polymorphism means you can call the same method on different objects, and each object responds in its own way based on its class implementation.
Real-World Analogy
Think of a universal remote control with a "power" button. When you press it, the TV turns on/off, the speaker turns on/off, and the fan turns on/off. Same action (press power), different behaviors based on the device.
Interactive Visualization
Polymorphism in Action
Types of Polymorphism
RuntimeMethod Overriding
- • Also called dynamic polymorphism
- • Child class redefines parent's method
- • Resolved at runtime based on object type
- • Requires inheritance
class Animal:
def speak(self): pass
class Dog(Animal):
def speak(self): # Override
return "Woof!"Compile-timeMethod Overloading
- • Also called static polymorphism
- • Same method name, different parameters
- • Resolved at compile time
- • Available in Java, C++ (not Python)
class Calc {
int add(int a, int b)
double add(double a, double b)
int add(int a, int b, int c)
}Code Implementation
# Polymorphism in Python
# Runtime Polymorphism (Method Overriding)
class Shape:
def area(self):
pass
def describe(self):
return f"I am a shape with area {self.area()}"
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self): # Override
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self): # Override
return 3.14159 * self.radius ** 2
class Triangle(Shape):
def __init__(self, base, height):
self.base = base
self.height = height
def area(self): # Override
return 0.5 * self.base * self.height
# Polymorphism in action - same interface, different behavior
shapes = [Rectangle(4, 5), Circle(3), Triangle(6, 4)]
for shape in shapes:
print(shape.describe())
# Output:
# I am a shape with area 20
# I am a shape with area 28.27431
# I am a shape with area 12.0
# Duck typing (Python-specific polymorphism)
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
class Duck:
def speak(self):
return "Quack!"
# No common parent needed in Python!
def make_speak(animal):
print(animal.speak())
make_speak(Dog()) # Woof!
make_speak(Cat()) # Meow!
make_speak(Duck()) # Quack!Duck Typing (Python/JS)
"If it walks like a duck and quacks like a duck, it's a duck."
In dynamically-typed languages like Python and JavaScript, polymorphism doesn't require inheritance. Any object with the required method can be used:
# No inheritance needed!
class Dog:
def speak(self):
return "Woof!"
class Robot: # Not related to Dog at all
def speak(self):
return "Beep boop!"
def make_noise(thing): # Works with any object that has speak()
print(thing.speak())
make_noise(Dog()) # Woof!
make_noise(Robot()) # Beep boop!Common Mistakes
Confusing overloading with overriding
Overloading = same name, different parameters (compile-time). Overriding = same signature, different class (runtime).
Missing virtual keyword in C++
In C++, you must mark methods as virtualfor runtime polymorphism to work. Without it, the base class method is always called.
Not using @Override annotation in Java
While optional, @Override helps catch typos. If you misspell the method name, the compiler will warn you instead of creating a new method.
Interview Tips
Test Your Knowledge
Polymorphism Quiz
Question 1 of 6What is polymorphism in OOP?