Method Overriding is one of the most important concepts in Object-Oriented Programming (OOP). It allows a child class to provide its own implementation of a method that already exists in the parent class.
Method overriding is closely related to inheritance and polymorphism, making Python programs more flexible and easier to extend.
In this tutorial, you will learn everything about method overriding in Python, including syntax, examples, advantages, and best practices.
What is Method Overriding?
Method overriding occurs when a child class defines a method with the same name as a method in its parent class.
When the method is called using an object of the child class, Python executes the child class version instead of the parent class version.
Why Use Method Overriding?
Method overriding helps:
- Customize inherited behavior
- Implement polymorphism
- Extend parent class functionality
- Improve code flexibility
- Support real-world object modeling
Basic Syntax
class Parent:
def show(self):
print("Parent Method")
class Child(Parent):
def show(self):
print("Child Method")The child class method replaces the parent class method.
Simple Method Overriding Example
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
dog = Dog()
dog.sound()Output
Dog barksPython executes the overridden method in the child class.
Understanding the Process
Animal
|
└── sound()
Dog
|
└── sound() ← overrides parent methodWhen:
dog.sound()Python first looks in the Dog class and finds the overridden method.
Parent Method Without Overriding
class Animal:
def sound(self):
print("Animal Sound")
class Dog(Animal):
pass
dog = Dog()
dog.sound()Output
Animal SoundSince no overriding occurs, Python uses the inherited method.
Real-World Example: Employee System
class Employee:
def work(self):
print("Employee Working")
class Developer(Employee):
def work(self):
print("Writing Python Code")
developer = Developer()
developer.work()Output
Writing Python CodeReal-World Example: Vehicle System
class Vehicle:
def start(self):
print("Vehicle Started")
class Car(Vehicle):
def start(self):
print("Car Started")
car = Car()
car.start()Output
Car StartedOverriding Multiple Methods
A child class can override multiple methods.
class Person:
def show_name(self):
print("Person Name")
def show_role(self):
print("Person Role")
class Student(Person):
def show_name(self):
print("Student Name")
def show_role(self):
print("Student Role")
student = Student()
student.show_name()
student.show_role()Output
Student Name
Student RoleUsing super() with Method Overriding
Sometimes you want to use the parent method while also adding new functionality.
The super() function allows access to the parent class method.
Example
class Animal:
def sound(self):
print("Animal Sound")
class Dog(Animal):
def sound(self):
super().sound()
print("Dog Bark")
dog = Dog()
dog.sound()Output
Animal Sound
Dog BarkExtending Parent Functionality
Instead of completely replacing a method, you can extend it.
class Employee:
def work(self):
print("Employee Working")
class Manager(Employee):
def work(self):
super().work()
print("Managing Team")
manager = Manager()
manager.work()Output
Employee Working
Managing TeamMethod Overriding and Constructors
Constructors can also be overridden.
Example
class Person:
def __init__(self):
print("Person Constructor")
class Student(Person):
def __init__(self):
print("Student Constructor")
student = Student()Output
Student ConstructorCalling Parent Constructor with super()
class Person:
def __init__(self):
print("Person Constructor")
class Student(Person):
def __init__(self):
super().__init__()
print("Student Constructor")
student = Student()Output
Person Constructor
Student ConstructorMethod Overriding and Polymorphism
Method overriding is the foundation of runtime polymorphism.
Example
class Animal:
def sound(self):
print("Animal Sound")
class Dog(Animal):
def sound(self):
print("Bark")
class Cat(Animal):
def sound(self):
print("Meow")
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()Output
Bark
MeowEach object executes its own overridden version.
Multi-Level Method Overriding
Methods can be overridden through multiple inheritance levels.
class A:
def show(self):
print("Class A")
class B(A):
def show(self):
print("Class B")
class C(B):
def show(self):
print("Class C")
obj = C()
obj.show()Output
Class CPython uses the nearest overridden method.
Method Resolution Order (MRO)
Python follows the Method Resolution Order (MRO) when searching for methods.
Example
class A:
pass
class B(A):
pass
class C(B):
pass
print(C.mro())Output
[<class '__main__.C'>,
<class '__main__.B'>,
<class '__main__.A'>,
<class 'object'>]Python searches classes in this order.
Practical Example: Online Payment System
class Payment:
def pay(self):
print("Processing Payment")
class CreditCard(Payment):
def pay(self):
print("Paid via Credit Card")
class PayPal(Payment):
def pay(self):
print("Paid via PayPal")
payments = [CreditCard(), PayPal()]
for payment in payments:
payment.pay()Output
Paid via Credit Card
Paid via PayPalPractical Example: Media Player
class Media:
def play(self):
print("Playing Media")
class Audio(Media):
def play(self):
print("Playing Audio")
class Video(Media):
def play(self):
print("Playing Video")
media_files = [Audio(), Video()]
for media in media_files:
media.play()Advantages of Method Overriding
1. Supports Polymorphism
Objects behave differently using the same method name.
2. Increases Flexibility
Child classes customize behavior.
3. Improves Code Reusability
Reuse parent class structures.
4. Better Maintainability
Updates can be made in specific subclasses.
Disadvantages of Method Overriding
1. Increased Complexity
Large inheritance hierarchies can be difficult to understand.
2. Debugging Challenges
Tracking overridden methods may require extra effort.
3. Improper Design Risks
Excessive overriding can make code harder to maintain.
Method Overriding vs Method Overloading
| Feature | Method Overriding | Method Overloading |
|---|---|---|
| Inheritance Required | Yes | No |
| Same Method Name | Yes | Yes |
| Same Parameters | Usually Yes | Different Parameters |
| Runtime Behavior | Yes | Limited in Python |
Common Mistakes
Mistake 1: Different Method Names
❌ Wrong
class Parent:
def show(self):
pass
class Child(Parent):
def display(self):
passThis is not overriding.
Mistake 2: Forgetting super()
When parent functionality is needed, use:
super().method_name()Mistake 3: Changing Method Purpose Completely
Avoid overriding methods with unrelated functionality.
Best Practices
- Keep method names consistent.
- Use
super()when extending functionality. - Override only when necessary.
- Maintain the original purpose of the method.
- Document overridden behavior clearly.
Method Overriding Summary
| Concept | Description |
| Method Overriding | Replacing inherited methods |
| Parent Class | Original implementation |
| Child Class | New implementation |
| super() | Access parent method |
| Runtime Polymorphism | Dynamic method execution |
| MRO | Method search order |
Conclusion
Method Overriding is a powerful feature of Python OOP that allows child classes to redefine inherited methods and provide specialized behavior.
You learned:
- What method overriding is
- How overriding works
- Using
super() - Overriding constructors
- Runtime polymorphism
- MRO concepts
- Real-world examples
- Best practices
Mastering method overriding is essential for building flexible, reusable, and scalable Python applications.
Practice Exercises
Exercise 1
Create an Animal class with a sound() method. Create Dog and Cat classes that override the method.
Exercise 2
Create a Vehicle class with a start() method. Override it in Car and Bike classes.
Exercise 3
Create a Payment class with a pay() method. Override it in CreditCard, PayPal, and CryptoPayment classes.


0 Comments