Introduction to polymorphism
Polymorphism is a fancy word that means many shapes. In programming, it refers to the ability of different classes to share the same method name but behave differently. So, even though different objects (like a dog or a cat) might use the same method name speak()
It’s like having a universal remote that can control different types of devices (TV, music player, etc.)—but each device responds in its own way when you press the button.
Two types of Polymorphism
-
Method Overloading (not common in Python but worth mentioning):
- This is when you have multiple methods with the same name but different parameters. The method behaves differently based on the arguments passed to it.
-
Method Overriding (which we already discussed!):
- This is when a child class overrides a method from the parent class to provide its own version.
But in Python, method overloading is not directly supported as it is in some other languages. Python focuses more on method overriding.
Polymorphism in action with method overriding
Let's focus on method overriding in polymorphism.
In this case, the same method name speak()
Example with animals:
# Parent class
class Animal:
def speak(self):
return "I make a sound"
# Child class Dog
class Dog(Animal):
def speak(self):
return "Woof!"
# Child class Cat
class Cat(Animal):
def speak(self):
return "Meow!"
In this example:
- The Animal class has a
method.speak()
- The Dog and Cat classes both override the
method to give their own versions.speak()
Using Polymorphism:
Now, let’s say you create both a Dog
Cat
speak()
# Create instances
dog = Dog()
cat = Cat()
# Call speak() on both
print(dog.speak()) # Output: Woof!
print(cat.speak()) # Output: Meow!
Both dog
and cat
are using the speak()
Why is Polymorphism useful?
- Flexibility: You can use the same method name for different types of objects, which makes the code cleaner and easier to read.
- Extendability: As you create more specific classes, you can easily add new behaviors to the same method name.
- Interchangeable objects: You can handle different objects (like
orDog
) in the same way, making your code more generic and reusable.Cat
Real-world example: Polymorphism in action
Imagine you have a game where you control different characters. All characters can attack, but each one has its own style of attack.
class Character:
def attack(self):
print("Generic attack!")
class Warrior(Character):
def attack(self):
print("Warrior swings a sword!")
class Mage(Character):
def attack(self):
print("Mage casts a spell!")
Now, when you call attack()
warrior = Warrior()
mage = Mage()
warrior.attack() # Output: Warrior swings a sword!
mage.attack() # Output: Mage casts a spell!
Even though both warrior
and mage
are characters and have an attack()
Key points to remember:
-
Polymorphism lets us use the same method name across different classes, but each class can have a different version of that method.
- It makes the code easier to maintain and extend.
- Method overriding (changing the behavior of inherited methods) is a common form of polymorphism.