DAY 29

Polymorphism 🎭🔄⚡

One command, many responses! The same method can behave differently depending on the object using it.

⏱ 15 mins
⚡ +50 XP
Polymorphism 🎭🔄⚡

Day 29: Polymorphism — One Command, Many Responses!

What's Polymorphism?

Press the volume button on a Samsung TV — it responds Samsung's way. Press the same button on a Sony TV — it responds Sony's way. Same button, different response. That's polymorphism! One method name, every object responds in its own unique way!

The Classic Example


class Dog:
    def speak(self):
        print("Dog says: Woof!")

class Cat:
def speak(self):
print("Cat says: Meow!")

animals = [Dog(), Cat()]

for animal in animals:
animal.speak()  

Output: Dog says: Woof! Cat says: Meow! Same method name speak(). Same loop calling it. But each object responds its own way. Python picks the right version automatically — no extra code needed!

Why It's Powerful

Without polymorphism you'd need to check which animal it is every time and call a different method. With polymorphism you just call speak() and let each object figure out its own response. Your code stays clean no matter how many animal types you add!

Real World Connection

Every payment app supports multiple methods — UPI, card, wallet. Each has a pay() method but each works differently. The checkout code just calls pay() and each payment type handles it its own way. In gaming, every weapon has an attack() method — sword, gun, magic all respond differently to the same command. One instruction, flexible system!

With Inheritance Too


class Animal:
    def speak(self):
        print("Some sound...")

class Dog(Animal):
def speak(self):
print("Woof!")

class Cat(Animal):
def speak(self):
print("Meow!")

class Bird(Animal):
def speak(self):
print("Tweet!")

animals = [Dog(), Cat(), Bird()]

for animal in animals:
animal.speak()  

Output: Woof! Meow! Tweet! All three inherit from Animal but each overrides speak() their own way. Add 100 more animals — the loop never changes. That's the beauty!

Common Mistakes

Mistake 1 — Forgetting parentheses when calling the method.


animal.speak    # WRONG — this is just a reference, nothing runs!
animal.speak()  # CORRECT — () actually calls it!

Mistake 2 — Different method names across classes.


class Cat:
    def sound(self):      # WRONG — name doesn't match Dog's speak()!
        print("Meow!")

class Cat:
def speak(self):      # CORRECT — same name, polymorphism works!
print("Meow!")  

Mini Challenge

Mini Challenge

Create three classes — Sword, Gun and MagicWand. Each has an attack() method that prints a different attack message. Put all three in a list and loop through them calling attack(). You just built the weapon system that every RPG game uses!

Quick Quiz

Q: What's the rule for polymorphism to work? A: Same method name across all classes — different behaviour inside each!

Q: If I add 10 more animal classes, do I change the loop? A: Never! The loop stays the same. Each new class just handles speak() its own way!

Q: What's the difference between animal.speak and animal.speak()? A: Without () — just a reference. With () — actually calls and runs it!

Key Takeaways

Key Takeaways

  • Polymorphism = same method name, different behaviour per object.
  • Python automatically calls the right version for each object.
  • Always use () when calling methods — without it, nothing runs!
  • Method names must match exactly across all classes for polymorphism to work.
  • One loop, unlimited object types — that's the power of flexible systems!

← Previous Lesson