How do I call a parent class's method from a child class in Python?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Calling a Parent Class's Method from a Child Class in Python

In Python, you can call a parent class's method from a child class using either the super() function or the parent class name directly. Here's how each approach works:

Using super()

The super() function is the most common and Pythonic way to call a method from the parent class. It automatically finds the parent class in the method resolution order (MRO) and calls the method.

Example:

class Parent: def greet(self): print("Hello from Parent") class Child(Parent): def greet(self): super().greet() # Calls the parent class's method print("Hello from Child") # Usage child = Child() child.greet()

Output:

Hello from Parent
Hello from Child

Explanation:

  • super().greet() calls the greet method from the Parent class.
  • After the parent class's method is executed, the child's own greet method continues.

Using the Parent Class Name Directly

You can also call the parent class's method directly by referencing the parent class name and passing self explicitly as the first argument.

Example:

class Parent: def greet(self): print("Hello from Parent") class Child(Parent): def greet(self): Parent.greet(self) # Explicitly calls the parent class's method print("Hello from Child") # Usage child = Child() child.greet()

Output:

Hello from Parent
Hello from Child

Explanation:

  • Parent.greet(self) directly invokes the greet method from the Parent class, bypassing super().

When to Use Each Approach

  1. Use super():

    • Recommended in most cases because it respects the method resolution order (MRO).
    • Essential in scenarios involving multiple inheritance, where super() ensures the correct parent class is called.
  2. Use Parent Class Name:

    • Useful for explicitly calling a specific parent class's method when you don't want to rely on MRO.
    • Can be necessary if the method isn't directly involved in multiple inheritance.

Example with Multiple Inheritance

Using super() ensures that all parent classes are called in the correct order.

class A: def greet(self): print("Hello from A") class B(A): def greet(self): print("Hello from B") super().greet() class C(B): def greet(self): print("Hello from C") super().greet() # Usage c = C() c.greet()

Output:

Hello from C
Hello from B
Hello from A

Explanation:

  • super() ensures that the methods are called in the correct MRO sequence (C -> B -> A).

Key Points

  • Use super() for clarity and compatibility with multiple inheritance.
  • Use the parent class name for explicit calls to a specific parent class.
  • Ensure that super() is correctly used in methods of all classes when working with multiple inheritance.

Happy coding!

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What are the unique behavioral interview questions?
How to approach interview coding questions?
Will Google hire freshers?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.