Polymorphism
What is Polymorphism?
Polymorphism is a big, fancy word that means “many shapes.” Think about how you use your hands: you can use them to write, to eat, or to play games. Even though your hands are the same, they can do different things. This is similar to how polymorphism works in programming. In Python, polymorphism allows us to use a single function or method in different ways for different data types or classes.
Real-World Example
Imagine you have different pets: a dog, a cat, and a bird. They all have a method called speak
, but they each make different sounds.
- The dog says “Woof!”
- The cat says “Meow!”
- The bird says “Tweet!”
Even though the action (speaking) is the same, the way each animal does it is different. This is polymorphism.
Polymorphism in Python
In Python, we use polymorphism with classes and methods. Let’s look at how we can create a simple example with animals.
Example Code
First, we’ll define a base class called Animal
with a method called speak
. Then we’ll create subclasses for ‘Dog
‘, 'Cat
‘, and ‘Bird
‘.
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
class Bird(Animal):
def speak(self):
return "Tweet!"
Now, let’s create instances of these classes and call the ‘speak
‘ method:
animals = [Dog(), Cat(), Bird()]
for animal in animals:
print(animal.speak())
Output:
Woof!
Meow!
Tweet!
Each animal object uses the ‘speak
‘ method in its own way, even though we’re calling the same method on each object. This is polymorphism in action!
Practice Questions
Let’s practice what we’ve learned with some questions and solutions.
Question 1
Create a base class called ‘Shape
‘ with a method called ‘area
‘. Then create subclasses ‘Circle
‘ and ‘Square
‘ that calculate the area for a circle (πr²) and a square (side²), respectively. Write code to demonstrate polymorphism by calling the ‘area
‘ method on both shapes.
Solution 1
import math
class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * (self.radius ** 2)
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side ** 2
shapes = [Circle(3), Square(4)]
for shape in shapes:
print(shape.area())
Output:
28.274333882308138
16
Question 2
Create a base class called ‘Vehicle'
with a method called ‘move
‘. Then create subclasses ‘Car
‘ and ‘Bike
‘ that define the ‘move
‘ method to print “The car is moving” and “The ‘bike’ is moving”, respectively. Demonstrate polymorphism by calling the ‘move
‘ method on both vehicles.
Solution 2
class Vehicle:
def move(self):
pass
class Car(Vehicle):
def move(self):
print("The car is moving")
class Bike(Vehicle):
def move(self):
print("The bike is moving")
vehicles = [Car(), Bike()]
for vehicle in vehicles:
vehicle.move()
Output:
The car is moving
The bike is moving
Polymorphism is a powerful concept in programming that allows objects of different classes to be treated as objects of a common superclass. This enables us to use a single method in different ways, depending on the object that calls it. By understanding and using polymorphism, we can write more flexible and reusable code.
Keep practicing with different examples to get comfortable with polymorphism. Happy coding!