What is Polymorphism?
One name, many forms -- the magic that makes OOP truly flexible.
Breaking Down the Word
Poly means "many." Morph means "forms." Together, polymorphism means "many forms." In OOP, it means that the same method name can behave differently depending on which object calls it.
Think of the word "open." You can open a door, open a book, open a conversation, or open a file. The action is the same word, but the behavior changes based on the context. That is polymorphism.
Animals That Speak Differently
Every animal can speak(), but each one speaks in its own way. The method name is the same; the output is different.
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
class Duck:
def speak(self):
return "Quack!"
animals = [Dog(), Cat(), Duck()]
for animal in animals:
print(animal.speak())
Output: Woof! / Meow! / Quack! -- same method call, three different results.
The Power: One Function, Many Types
Polymorphism lets you write a single function that works with any object -- as long as that object has the expected method. You do not need to know the exact class.
def make_sound(animal):
print(animal.speak())
make_sound(Dog()) # Woof!
make_sound(Cat()) # Meow!
The function make_sound() does not check what type of animal it receives. It just calls speak(). If the object has that method, it works. That is the beauty of polymorphism.
Real-World Example: Payment Processing
Imagine an e-commerce site that accepts multiple payment methods. Each method has a process() method, but each one works differently internally.
class CreditCard:
def process(self, amount):
print(f"Charging ${amount} to credit card")
class PayPal:
def process(self, amount):
print(f"Sending ${amount} via PayPal")
class BankTransfer:
def process(self, amount):
print(f"Transferring ${amount} from bank")
# One function handles all payment types
def checkout(payment_method, amount):
payment_method.process(amount)
checkout(CreditCard(), 49.99)
checkout(PayPal(), 29.99)
checkout(BankTransfer(), 99.99)
Key OOP Concept
Polymorphism = same interface, different implementations. You define a common method name (the interface), and each class provides its own version (the implementation). Code that uses the interface does not need to know the details.
Best Practices
- Use consistent method names across related classes so polymorphism works naturally.
- Write functions that depend on behavior (methods), not on specific class types.
- Keep the method signatures (parameters) the same across polymorphic classes.
Common Mistakes
- Using different method names in related classes (e.g.,
bark()andmeow()instead of a sharedspeak()). - Checking the type of an object with
isinstance()before calling a method -- that defeats the purpose of polymorphism. - Assuming polymorphism requires inheritance -- in Python, it does not (as you will see with duck typing).
Practice Exercises
Exercise 1: Vehicle Sounds
Create classes Car, Motorcycle, and Truck, each with a honk() method. Loop through a list of vehicles and call honk() on each.
Exercise 2: Shape Describer
Create classes Circle, Square, and Triangle, each with a describe() method that returns a string like "I am a circle." Write a function that accepts any shape and prints its description.
Exercise 3: File Exporters
Create classes CSVExporter, JSONExporter, and PDFExporter, each with an export(data) method that prints which format the data is being exported as.
Summary
- Polymorphism means "many forms" -- same method name, different behavior.
- It lets you write flexible code that works with any compatible object.
- Real-world systems like payment processing rely on polymorphism.
- Focus on shared method names, not on checking object types.