Polymorphism and Method Overriding in Python
What is Polymorphism?
Polymorphism is a characteristic of an object-oriented programming language that allows an object to have multiple forms. In other words, it allows the same code to be used to process objects of different types or classes. It is a feature which allows classes to share the same interface and also to use methods that are already defined in their parent classes.
What is Method Overriding?
Method overriding is a feature of object-oriented programming that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. It is often used to provide specific functionality for a particular use case or situation.
Example of Polymorphism and Method Overriding in Python
Example 1:
Let’s take an example of a shape class and its subclasses, Rectangle and Circle. The shape class has a method called area() which calculates the area of the shape. The Rectangle and Circle classes will both have their own implementation of the area() method which calculates the area of the specific shape. This is an example of polymorphism and method overriding.
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius**2
rectangle = Rectangle(10, 20)
print(rectangle.area()) # 200
circle = Circle(10)
print(circle.area()) # 314
Example 2:
Let’s take another example of a class called Animal and its subclasses, Dog and Cat. The Animal class has a method called speak() which prints a message. The Dog and Cat classes will each have their own implementation of the speak() method which prints a message specific to the animal. This is an example of polymorphism and method overriding.
class Animal:
def speak(self):
print('I am an animal')
class Dog(Animal):
def speak(self):
print('Woof!')
class Cat(Animal):
def speak(self):
print('Meow!')
dog = Dog()
dog.speak() # Woof!
cat = Cat()
cat.speak() # Meow!
Example 3:
Let’s take another example of a class called Vehicle and its subclasses, Car and Truck. The Vehicle class has a method called get_info() which prints information about the vehicle. The Car and Truck classes will each have their own implementation of the get_info() method which prints information specific to the vehicle. This is an example of polymorphism and method overriding.
class Vehicle:
def get_info(self):
print('This is a vehicle')
class Car(Vehicle):
def get_info(self):
print('This is a car')
class Truck(Vehicle):
def get_info(self):
print('This is a truck')
car = Car()
car.get_info() # This is a car
truck = Truck()
truck.get_info() # This is a truck
Tips for Using Polymorphism and Method Overriding in Python
- Remember to use the same method name when overriding a method in the subclass.
- It is important to use the correct data type when overriding a method in the subclass.
- Be sure to call the superclass's method if it needs to be used in the subclass.
- Polymorphism and method overriding can be used to make code more readable and maintainable.
- It is important to consider the performance implications of using polymorphism and method overriding.
- Make sure to test any code that uses polymorphism and method overriding to ensure it works as expected.