Method Overriding in Python

Last Updated : 1 Jul, 2026

Method overriding occurs when a child class defines a method with the same name as a method in its parent class. This allows the child class to provide its own implementation while retaining the inheritance relationship. It is commonly used to customize or extend the behavior of inherited methods.

In this example, the child class overrides the display() method defined in the parent class.

Python
class Animal:
    def display(self):
        print("This is an animal")

class Dog(Animal):
    def display(self):
        print("This is a dog")

obj = Dog()
obj.display()

Output
This is a dog

Explanation:

  • Animal class contains a method named display(). Dog class inherits from Animal and defines its own display() method.
  • Since the child class has a method with the same name, it overrides the parent class method.
  • When obj.display() is called, Python executes the version defined in the Dog class instead of the one in the Animal class.

Method Overriding with Constructors

When a child class defines its own constructor (__init__()), the parent class constructor is not called automatically. We can use super().__init__() to execute the parent constructor and initialize any attributes defined there before adding child-specific behavior.

Example: In this example, the child class overrides both the constructor and a method inherited from the parent class.

Python
class Employee:
    def __init__(self):
        self.role = "Employee"

    def display(self):
        print("Role:", self.role)

class Manager(Employee):
    def __init__(self):
        super().__init__()
        self.role = "Manager"

    def display(self):
        print("Role:", self.role)

e1 = Employee()
e2 = Manager()
e1.display()
e2.display()

Output
Role: Employee
Role: Manager

Explanation:

  • super().__init__() calls the parent class constructor.
  • Both classes have a display() method, but the child class provides its own implementation.
  • When display() is called on a Manager object, the overridden version executes.

Overriding in Multiple Inheritance

In multiple inheritance, a child class inherits from more than one parent class. The child can override methods from any parent while still accessing methods that are not overridden.

Example: Here, the child class overrides one method and inherits another unchanged.

Python
class Teacher:
    def introduce(self):
        print("I am a Teacher")

class Writer:
    def write(self):
        print("Writing an article")

class Author(Teacher, Writer):
    def introduce(self):
        print("I am an Author")

obj = Author()
obj.introduce()
obj.write()

Output
I am an Author
Writing an article

Explanation:

  • Author inherits from both Teacher and Writer.
  • introduce() is overridden in the child class.
  • write() is inherited directly from Writer.
  • The overridden method takes priority over the parent version.

Overriding in Multilevel Inheritance

In multilevel inheritance, a class inherits from another child class. A method can be overridden at any level, and the most specific implementation is executed.

Example: The grandchild class overrides a method inherited through multiple levels.

Python
class Vehicle:
    def start(self):
        print("Vehicle started")

class Car(Vehicle):
    def start(self):
        print("Car started")

class SportsCar(Car):
    def start(self):
        print("Sports Car started")

obj = SportsCar()
obj.start()

Output
Sports Car started

Explanation:

  • SportsCar inherits from Car, which inherits from Vehicle.
  • All three classes define a start() method.
  • Python executes the method from the most derived class (SportsCar).

Method Calls Inside Parent Class

A parent class method can call another method using self. If that second method is overridden in the child class, Python automatically executes the child class version. This behavior is a key part of runtime polymorphism.

Example: In this example, the parent class method calls another method that is overridden in the child class.

Python
class Report:
    def generate(self):
        print("Generating report...")
        self.display()

    def display(self):
        print("Displaying basic report")

class SalesReport(Report):
    def display(self):
        print("Displaying sales report")

obj = SalesReport()
obj.generate()

Output
Generating report...
Displaying sales report

Explanation:

  • generate() is defined in the parent class. Inside generate(), the statement self.display() is executed.
  • Since display() is overridden in SalesReport, Python calls the child class version instead of the parent version.
  • This happens because self refers to the actual object (SalesReport object), not the class where the method was originally defined.
  • This behavior is one of the main reasons method overriding is useful for implementing polymorphism.

Calling Parent Method Using Class Name

Sometimes we want to extend a parent method instead of completely replacing it. One way is to call the parent method directly using the parent class name.

Example: The child method calls the parent version before executing its own code.

Python
class Notification:
    def send(self):
        print("Sending notification")

class Email(Notification):
    def send(self):
        Notification.send(self)
        print("Sending email")

obj = Email()
obj.send()

Output
Sending notification
Sending email

Explanation:

  • Notification.send(self) explicitly calls the parent class method.
  • After executing the parent logic, the child method continues.
  • This approach works but requires directly specifying the parent class name.

Calling Parent Method Using super()

The preferred way to access parent class methods is by using super(). It improves readability and works well in inheritance hierarchies.

Example: The child method extends the parent method using super().

Python
class Payment:
    def process(self):
        print("Processing payment")

class OnlinePayment(Payment):
    def process(self):
        super().process()
        print("Generating receipt")

obj = OnlinePayment()
obj.process()

Output
Processing payment
Generating receipt

Explanation:

  • super().process() calls the parent class method.
  • After the parent method completes, the child-specific code runs.
  • super() is generally preferred over directly using the parent class name because it supports more complex inheritance structures.
Comment