πŸ” Python OOP β€” Method Overriding

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

πŸ’‘ Overriding = Same method name, different behavior in the child class
πŸ’‘ 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 OverridingMethod Overloading
Same name in parent & childSame name, different parameters
Runtime polymorphismCompile-time polymorphism (not in Python)
Used in inheritancePython 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 πŸŽ‰

>>β€œMethod Overriding allows flexibility β€” giving child classes the power to define their own identity.” ✨

You now fully understand Method Overriding in Python! Want the next topic? Try Method Overloading, MRO, Polymorphism, or Magic Methods. Just tell me! 😊