Have you ever seen your teacher write with a pen, then with a marker, and then type on a keyboard?
All three are different, but the action is the same: writing ✍️
This is exactly what Polymorphism means in Python — one function or method can work in many different ways depending on the object!
🎭 What is Polymorphism?
Polymorphism is a fancy word that comes from Greek:
-
"Poly" means many
-
"Morph" means forms
So Polymorphism = many forms 🌀
In Python, polymorphism allows us to:
-
Use the same function name for different objects
-
Write less code and do more work
🧒 Real-Life Example: Animals Making Sounds
Let’s say we have different animals, and they all make sounds — but each makes a different sound.
Now let’s call their speak() method:
💬 Output:
🎉 Even though the method name is the same (speak()), each animal does something different. That’s Polymorphism in action!
🔁 Polymorphism with Functions
You can also write a function that works with many object types:
✅ The same function behaves differently depending on what you pass into it!
🎮 Mini Challenge Time!
Create two classes:
-
Car→ methodmove()→ prints"Driving on the road 🚗" -
Boat→ methodmove()→ prints"Sailing on water 🚤"
Then write a loop to call move() for both.
Can you do it? Try it out!
🧠 Why Polymorphism is Cool:
| Benefit | What it Means |
|---|---|
| ✅ Less Repetition | No need to write same function again |
| 🔁 Reusability | Use same function for different objects |
| 📦 Clean Code | Easier to read and manage your program |
🔮 Summary
| Term | What It Means |
|---|---|
| Polymorphism | One name, many behaviors |
| Method | A function inside a class |
| Class | A blueprint to create objects |
🧠 Real-World Examples
-
print()works for text, numbers, and even lists!
That’s built-in polymorphism in Python!
🚀 Coming Next
-
✨ Encapsulation — hide your data like a secret vault
-
🧬 Inheritance — share features between parent and child classes
🎁 Bonus: Try This!