Introduction
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 its parent class.
When inheritance is used, a child class automatically inherits methods and attributes from the parent class. However, there are situations where the child class needs to perform a different action than the parent class. In such cases, method overriding becomes useful.
For example, consider an Animal class with a method called sound(). Different animals produce different sounds. A dog barks, a cat meows, and a cow moos. Instead of using the same method implementation for all animals, each child class can override the sound() method according to its behavior.
What is Method Overriding in Python?
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 a child class object, Python executes the child class version instead of the parent class version.
Example:
class Parent:
def display(self):
print("Parent Method")
class Child(Parent):
def display(self):
print("Child Method")
obj = Child()
obj.display()
Output:
Explanation:
- Both classes contain a method named display().
- The child class overrides the parent method.
- Python calls the child method because the object belongs to the child class.
Syntax
Basic syntax:
class Parent:
def method_name(self):
pass
class Child(Parent):
def method_name(self):
pass
The method name and parameters should generally match the parent method.
Basic Example of Method Overriding
class Animal:
def sound(self):
print("Animal Sound")
class Dog(Animal):
def sound(self):
print("Dog Barks")
dog = Dog()
dog.sound()
Output:
Explanation:
The Dog class overrides the sound() method inherited from Animal.
Method Overriding with Multiple Child Classes
class Animal:
def sound(self):
print("Animal Sound")
class Dog(Animal):
def sound(self):
print("Bark")
class Cat(Animal):
def sound(self):
print("Meow")
class Cow(Animal):
def sound(self):
print("Moo")
dog = Dog()
cat = Cat()
cow = Cow()
dog.sound()
cat.sound()
cow.sound()
Output:
Meow
Moo
Each child class provides its own implementation.
Method Overriding and Inheritance
Method overriding only works when inheritance exists.
Example:
class Person:
def role(self):
print("General Person")
class Student(Person):
def role(self):
print("Student")
student = Student()
student.role()
Output:
The child class method replaces the parent method.
Accessing Parent Method Using super()
Sometimes we want to use both the parent method and the child method.
Python provides the super() function for this purpose.
Example:
class Person:
def display(self):
print("Person Information")
class Student(Person):
def display(self):
super().display()
print("Student Information")
student = Student()
student.display()
Output:
Student Information
Explanation:
- super().display() calls the parent method.
- The child method adds additional functionality.
Overriding Constructors (init)
Constructors can also be overridden.
Parent Class
class Person:
def __init__(self):
print("Person Constructor")
Child Class
class Student(Person):
def __init__(self):
print("Student Constructor")
student = Student()
Output:
The parent constructor is overridden.
Calling 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:
Student Constructor
This ensures both constructors execute.
Real-Life Examples:
1. Employee Management System
class Employee:
def work(self):
print("Employee Working")
class Manager(Employee):
def work(self):
print("Manager Managing Team")
class Developer(Employee):
def work(self):
print("Developer Writing Code")
manager = Manager()
developer = Developer()
manager.work()
developer.work()
Output:
Developer Writing Code
Explanation:
Different employee roles perform different tasks.
2. Payment System
class Payment:
def process_payment(self):
print("Processing Payment")
class CreditCard(Payment):
def process_payment(self):
print("Processing Credit Card Payment")
class PayPal(Payment):
def process_payment(self):
print("Processing PayPal Payment")
Usage:
payment1 = CreditCard()
payment2 = PayPal()
payment1.process_payment()
payment2.process_payment()
Output:
Processing PayPal Payment
This is a common real-world use of method overriding.
Method 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:
Meow
The same method call behaves differently depending on the object type.
Method Overriding vs Method Overloading
| Feature | Method Overriding | Method Overloading |
|---|---|---|
| Occurs In | Parent and Child Classes | Same Class |
| Requires Inheritance | Yes | No |
| Method Name | Same | Same |
| Parameters | Usually Same | Different |
| Supported in Python | Yes | Limited Support |
Advantages of Method Overriding
| Advantage | Description |
|---|---|
| Flexibility | Child classes customize behavior |
| Reusability | Parent code can be reused |
| Polymorphism | Supports runtime polymorphism |
| Scalability | Easier to extend applications |
| Maintainability | Cleaner code organization |
Common Mistakes
1. Forgetting Inheritance
Incorrect:
class Parent:
def show(self):
pass
class Child:
def show(self):
pass
This is not method overriding because inheritance is missing.
Correct:
class Child(Parent):
2. Using Different Method Names
Incorrect:
class Parent:
def display(self):
pass
class Child(Parent):
def show(self):
pass
The method is not overridden because the names differ.
3. Forgetting super() When Needed
Incorrect:
class Student(Person):
def __init__(self):
print("Student")
This skips parent initialization.
Better:
super().__init__()
4. Changing Method Behavior Unnecessarily
Override methods only when behavior truly differs.
5. Creating Deep Inheritance Chains
Avoid excessive inheritance levels because they make overriding difficult to track.
Conclusion
Method overriding is a powerful feature of Object-Oriented Programming in Python that allows child classes to provide specialized implementations of parent class methods. It enhances flexibility, promotes code reuse, and serves as the foundation for runtime polymorphism.