Introduction π
**Method Overriding** allows a child class to provide a new implementation for a method that is already defined in its parent class. It is a key part of **runtime polymorphism** in Python.
Note
π‘ Python chooses which version to run at **runtime**
1. Basic Example of Method Overriding π§±
basic_overriding.py
class Parent:
def show(self):
print("Parent show method")
class Child(Parent):
def show(self):
print("Child show method")
c = Child()
c.show()β Child method replaces (overrides) the parent's method
β Demonstrates runtime polymorphism
2. Calling Parent Method Using super() π
super_overriding.py
class Parent:
def show(self):
print("Parent show method")
class Child(Parent):
def show(self):
super().show() # calling parent method
print("Child show method")
c = Child()
c.show()β Child extends parent behavior instead of replacing it
3. Overriding in Multilevel Inheritance π§¬
multilevel_overriding.py
class A:
def greet(self):
print("Hello from A")
class B(A):
def greet(self):
print("Hello from B")
class C(B):
def greet(self):
print("Hello from C")
c = C()
c.greet()β Most derived class (C) overrides all previous implementations
4. Overriding with Different Behavior π
different_behavior.py
class Shape:
def area(self):
print("Area not defined")
class Circle(Shape):
def area(self):
return 3.14 * 5 * 5
print(Circle().area())β Parent provides a default or placeholder method
β Child provides a meaningful implementation
5. Overriding Abstract Methods π
abstract_overriding.py
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Bark"
print(Dog().sound())β Abstract classes require overriding
β Child must define the method
6. Real-World Example β Payment System π³
payment_overriding.py
class Payment:
def pay(self, amt):
print("Processing payment")
class CardPayment(Payment):
def pay(self, amt):
print(f"Paid {amt} using Card")
class UpiPayment(Payment):
def pay(self, amt):
print(f"Paid {amt} using UPI")
for method in [CardPayment(), UpiPayment()]:
method.pay(500)β Same method name β different behaviors
β Achieves runtime polymorphism
7. Overriding and MRO (Multiple Inheritance) π
mro_overriding.py
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
class C(A):
def show(self):
print("C")
class D(B, C):
pass
D().show()β Output depends on MRO
β MRO for D = [D, B, C, A, object]
8. Method Overriding vs Method Overloading βοΈ
| Method Overriding | Method Overloading |
|---|---|
| Same name in parent & child | Same name, different parameters |
| Runtime polymorphism | Compile-time polymorphism (not in Python) |
| Used in inheritance | Python uses default parameters instead |
9. Best Practices π‘
- β Override only when necessary
- β Use super() to retain parent logic
- β Keep overridden methods simple and consistent
- β Use meaningful method names
- β Avoid changing method signatures unnecessarily
10. Full Example β Vehicle System π
vehicle_overriding.py
class Vehicle:
def start(self):
print("Vehicle started")
class Car(Vehicle):
def start(self):
print("Car started with key")
class Bike(Vehicle):
def start(self):
print("Bike started with kick")
v = [Car(), Bike()]
for veh in v:
veh.start()Conclusion π
You now fully understand Method Overriding in Python! Want the next topic? Try Method Overloading, MRO, Polymorphism, or Magic Methods. Just tell me! π