Open In App

What is the Proper Way to Call a Parent's Class Method Inside a Class Method?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In object-oriented programming, calling a parent class method inside a class method is a common practice, especially when you want to extend or modify the functionality of an inherited method. This process is known as method overriding. Here's how to properly call a parent class method inside a class method in Python.

Basic Syntax Using super()

The recommended way to call a parent class method is to use the super() function. This approach ensures that the method resolution order (MRO) is followed correctly, especially in the context of multiple inheritance.

Using super() in Single Inheritance

Here's a basic example demonstrating the use of super() in a single inheritance scenario:

Python
class Parent:
    def display(self):
        print("This is the parent class method.")

class Child(Parent):
    def display(self):
        # Call the parent class method
        super().display()
        print("This is the child class method.")

# Create an instance of the child class
child_instance = Child()
child_instance.display()

Output:

This is the parent class method.
This is the child class method.

In this example, the Child class overrides the display method of the Parent class. Inside the Child class's display method, super().display() calls the display method of the Parent class.

Using super() in Multiple Inheritance

In cases of multiple inheritance, super() ensures that the correct method resolution order is followed. Here's an example:

Python
class A:
    def display(self):
        print("This is class A method.")

class B(A):
    def display(self):
        print("This is class B method.")
        super().display()

class C(A):
    def display(self):
        print("This is class C method.")
        super().display()

class D(B, C):
    def display(self):
        print("This is class D method.")
        super().display()

# Create an instance of class D
d_instance = D()
d_instance.display()

Output:

This is class D method.
This is class B method.
This is class C method.
This is class A method.

In this example, class D inherits from both class B and class C, which in turn inherit from class A. The super() function ensures that the method calls follow the correct method resolution order, as defined by Python's MRO.

Using the Parent Class Name Directly

An alternative to using super() is to call the parent class method directly using the parent class name. This method is less flexible and is generally not recommended, especially in complex inheritance hierarchies. Here's an example:

Python
class Parent:
    def display(self):
        print("This is the parent class method.")

class Child(Parent):
    def display(self):
        # Call the parent class method using the parent class name
        Parent.display(self)
        print("This is the child class method.")

# Create an instance of the child class
child_instance = Child()
child_instance.display()

Output:

This is the parent class method.
This is the child class method.

When to Use super()

  • Single Inheritance: Simplifies method calls and makes the code more readable.
  • Multiple Inheritance: Ensures the correct method resolution order is followed, which is critical for maintaining consistent behavior.

Conclusion

The proper way to call a parent's class method inside a class method is to use the super() function. This approach provides flexibility, readability, and correctness, particularly in the context of complex inheritance hierarchies. While calling the parent class method directly using the parent class name is an option, it is less recommended due to its limitations in multiple inheritance scenarios.


Article Tags :
Practice Tags :

Similar Reads