Method Overriding in Python 🔄🐍

Method Overriding allows a child class to provide a specific implementation of a method that is already defined in its parent class.
✅ Used when we want to modify or extend a parent class’s method.
✅ The method in the child class must have the same name as in the parent class.
✅ Python follows the Method Resolution Order (MRO) to decide which method to call.
1️⃣ Basic Example of Method Overriding
class Animal:
def speak(self):
return "Animal makes a sound"
class Dog(Animal): # Child class
def speak(self): # Overriding the method
return "Woof! Woof!"
# Creating objects
animal = Animal()
dog = Dog()
print(animal.speak()) # Output: Animal makes a sound
print(dog.speak()) # Output: Woof! Woof! (Overridden method)
🔹 The speak() method in Dog overrides the one in Animal.
🔹 When speak() is called on a Dog object, Python executes the overridden method in Dog instead of Animal.
2️⃣ Overriding with super() to Call Parent Method
📌 We can use super() to call the parent class’s version of the overridden method.
class Animal:
def speak(self):
return "Animal makes a sound"
class Dog(Animal):
def speak(self):
original_sound = super().speak() # Call parent method
return original_sound + " but Dog says Woof! Woof!"
dog = Dog()
print(dog.speak())
# Output: Animal makes a sound but Dog says Woof! Woof!
🔹 super().speak() calls the parent class’s method, then adds extra behavior.
3️⃣ Example: Overriding the __init__() Constructor
📌 The constructor (__init__()) can also be overridden to extend its functionality.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age) # Call Parent constructor
self.grade = grade # New attribute
student = Student("Alice", 20, "A")
print(student.name) # Output: Alice
print(student.age) # Output: 20
print(student.grade) # Output: A
🔹 Student overrides __init__(), calls super().__init__(), and adds a new attribute grade.
4️⃣ Overriding Methods in Multiple Inheritance
📌 When multiple parent classes have the same method, Python follows the MRO (Method Resolution Order).
class A:
def show(self):
return "A's show() method"
class B:
def show(self):
return "B's show() method"
class C(A, B): # Inherit from A and B
pass
c = C()
print(c.show()) # Output: A's show() method (A comes first in MRO)
🔹 Since A appears first in the inheritance list (class C(A, B)), its show() method is executed.
🔹 To override both and customize behavior, we define show() in C:
class C(A, B):
def show(self):
return "C's show() method (Overridden)"
c = C()
print(c.show()) # Output: C's show() method (Overridden)
5️⃣ When to Use Method Overriding?
✅ When the child class needs a different behavior for an inherited method.
✅ When extending a parent class without modifying it directly.
✅ When using polymorphism (e.g., different animals making different sounds).
6️⃣ Summary Table
Feature Description
Method Overriding A child class redefines a method from its parent class.
super() Calls the parent class’s method inside the overridden method.
Overriding __init__() Allows adding new attributes while keeping the parent’s functionality.
MRO in Overriding Python follows the Method Resolution Order (MRO) when multiple parents have the same
method.
Date: 2025-03-24 00:00:00.000000