What is method overriding?
Method overriding in Python is a fundamental concept in object-oriented programming where a subclass (child class) provides a specific implementation for a method that is already defined in its superclass (parent class). When an object of the subclass calls this method, the subclass's version of the method is executed instead of the superclass's version.
Definition and Concept
Method overriding allows a subclass to change the behavior of an inherited method without modifying the superclass. It's a way for a child class to provide a more specific or custom implementation for a method that it inherits from its parent, while keeping the same method signature (name and parameters). This is a key aspect of polymorphism in object-oriented programming, enabling different classes to respond uniquely to the same method call.
How it Works
When an instance of a subclass calls a method that is defined in both the subclass and its superclass, Python's Method Resolution Order (MRO) comes into play. The MRO dictates the order in which base classes are searched for a method. For an object of the subclass, the interpreter will first look for the method in the subclass. If found, that version is executed. If not found, it then looks in the direct parent class, and so on, up the inheritance hierarchy.
Example of Method Overriding
Consider a base class Animal with a speak method. A derived class Dog might want to implement its own version of speak to reflect the sound a dog makes, rather than the generic sound an animal makes.
class Animal:
def speak(self):
return "The animal makes a sound."
class Dog(Animal):
def speak(self):
return "Woof! Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
animal = Animal()
dog = Dog()
cat = Cat()
print(animal.speak())
print(dog.speak())
print(cat.speak())
Output of the above code:
The animal makes a sound.
Woof! Woof!
Meow!
In this example, both Dog and Cat classes override the speak method inherited from the Animal class, providing their specific implementations.
Calling the Parent Class Method (using `super()`)
Sometimes, when overriding a method, you might want to extend or augment the parent's method rather than completely replace it. Python provides the super() function to call methods from the parent (superclass) within the overridden method of the child class. This is particularly useful for adding functionality while retaining the base behavior.
class Vehicle:
def __init__(self, brand):
self.brand = brand
def accelerate(self):
return f"{self.brand} is accelerating."
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand) # Call parent's __init__
self.model = model
def accelerate(self):
# Call the parent's accelerate method and add more functionality
base_acceleration = super().accelerate()
return f"{base_acceleration} The {self.model} goes faster!"
my_car = Car("Toyota", "Camry")
print(my_car.accelerate())
Output of the above code:
Toyota is accelerating. The Camry goes faster!
Here, the Car class's accelerate method first calls the accelerate method of its parent Vehicle using super() and then adds its specific message.
Key Takeaways
- Method overriding allows a subclass to provide a specific implementation for an inherited method.
- The method signature (name and parameters) must be the same as in the parent class.
- It's a core principle of polymorphism, enabling objects of different classes to respond differently to the same method call.
super()can be used within an overridden method to call the parent class's implementation of that method, allowing for extension rather than complete replacement.