Call Parent class method – Python
Last Updated :
14 Apr, 2025
In object-oriented programming in Python, the child class will inherit all the properties and methods of the parent class when the child class inherits the parent class. But there may be some cases when the child class has to call the methods of the parent class directly. Python has an easy and effective solution to call the parent class methods and both parent and child classes can work together without any problems. This is especially useful when we override a method in the child class but still want to use the parent class’s version of the method.
Example:
Python
class cls:
def __init__(self, fname, mname, lname):
self.firstname = fname
self.middlename = mname
self.lastname = lname
def print(self):
print(self.firstname, self.middlename, self.lastname)
x = cls("Geeks", "for", "Geeks")
x.print()
Explanation: This code defines a class cls with a constructor to initialize firstname, middlename and lastname. The print method outputs the full name. An object x is created with names “Geeks”, “for” and “Geeks” and the print method displays “Geeks for Geeks”.
Syntax for Calling Parent Class Method in Python
There are a couple of ways we can call the parent class method in Python, depending on whether we’re using super() or directly referencing the parent class.
1. Using super()
The super() function allows us to call methods from the parent class. The syntax is:
super().method_name(args)
2. Calling Parent Class Method Directly
Alternatively, we can call the parent class method explicitly by referencing the class name directly.
ParentClass.method_name(self, args)
Understanding Inheritance in Python
In simpler terms, inheritance is the concept by which one class (commonly known as child class or sub class) inherits the properties from another class (commonly known as Parent class or super class). But have we ever wondered about calling the functions defined inside the parent class with the help of child class? Well this can done using Python. we just have to create an object of the child class and call the function of the parent class using dot(.) operator.
Example:
Python
class Parent:
def show(self):
print("Inside Parent class")
class Child(Parent):
def display(self):
print("Inside Child class")
obj = Child()
obj.display()
obj.show()
OutputInside Child class
Inside Parent class
Explanation: This code defines a parent class Parent with a method show that prints “Inside Parent class.” A child class Child inherits from Parent and has its own method display that prints “Inside Child class.” An object obj of the Child class is created and both display and show methods are called, printing “Inside Child class” followed by “Inside Parent class.”
Calling Parent class method after method overriding
Method overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. Parent class methods can also be called within the overridden methods. This can generally be achieved by two ways.
Using Classname
Parent’s class methods can be called by using the Parent classname.method inside the overridden method.
Example:
Python
class Parent():
def show(self):
print("Inside Parent")
class Child(Parent):
def show(self):
Parent.show(self)
print("Inside Child")
obj = Child()
obj.show()
OutputInside Parent
Inside Child
Explanation: This code defines a Parent class with a method show that prints “Inside Parent.” The Child class inherits from Parent and overrides the show method. Inside the child class’s show method, it calls the parent class’s show method using Parent.show(self) and then prints “Inside Child.” An object obj of the Child class is created and the show method is called, printing “Inside Parent” followed by “Inside Child.”
Using Super()
Python super() function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by ‘super’.
Example 1:
Python
class Parent():
def show(self):
print("Inside Parent")
class Child(Parent):
def show(self):
super().show()
print("Inside Child")
obj = Child()
obj.show()
OutputInside Parent
Inside Child
Explanation: This code defines a Parent class with a show method. The Child class overrides the show method and calls the parent’s show method using super(). When obj.show() is called, it prints “Inside Parent” followed by “Inside Child.”
Example 2:
Python
class GFG1:
def __init__(self):
print('HEY !!!!!! GfG I am initialised(Class GEG1)')
def sub_GFG(self, b):
print('Printing from class GFG1:', b)
# class GFG2 inherits the GFG1
class GFG2(GFG1):
def __init__(self):
print('HEY !!!!!! GfG I am initialised(Class GEG2)')
super().__init__()
def sub_GFG(self, b):
print('Printing from class GFG2:', b)
super().sub_GFG(b + 1)
# class GFG3 inherits the GFG1 ang GFG2 both
class GFG3(GFG2):
def __init__(self):
print('HEY !!!!!! GfG I am initialised(Class GEG3)')
super().__init__()
def sub_GFG(self, b):
print('Printing from class GFG3:', b)
super().sub_GFG(b + 1)
# main function
if __name__ == '__main__':
gfg = GFG3()
gfg.sub_GFG(10)
Output
HEY !!!!!! GfG I am initialised(Class GEG3)
HEY !!!!!! GfG I am initialised(Class GEG2)
HEY !!!!!! GfG I am initialised(Class GEG1)
Printing from class GFG3: 10
Printing from class GFG2: 11
Printing from class GFG1: 12
Explanation: This code demonstrates multiple inheritance where GFG2 and GFG3 inherit from GFG1. Each class overrides the sub_GFG method and super() is used to call the parent class method. When an object of GFG3 is created and sub_GFG(10) is called, it prints messages from all three classes, showcasing method resolution in multiple inheritance.
Related Articles:
Similar Reads
classmethod() in Python
The classmethod() is an inbuilt function in Python, which returns a class method for a given function. This means that classmethod() is a built-in Python function that transforms a regular method into a class method. When a method is defined using the @classmethod decorator (which internally calls c
8 min read
Python Metaclass __new__() Method
In Python, metaclasses provide a powerful way to customize the creation of classes. One essential method in metaclasses is __new__, which is responsible for creating a new instance of a class before __init__ is called. Understanding the return value of __new__ in metaclasses is crucial for implement
3 min read
Print Objects of a Class in Python
In object-oriented programming (OOP), an object is an instance of a class. A class serves as a blueprint, defining the structure and behavior of objects, while an instance is a specific copy of the class with actual values. When an object is created, the class is said to be instantiated. All instanc
4 min read
Python | Decimal max() method
Decimal#max() : max() is a Decimal class method which compares the two Decimal values and return the max of two. Syntax: Decimal.max() Parameter: Decimal values Return: the max of two. Code #1 : Example for max() method # Python Program explaining # max() method # loading decimal library from decima
2 min read
Python | Decimal min() method
Decimal#min() : min() is a Decimal class method which compares the two Decimal values and return the min of two. Syntax: Decimal.min() Parameter: Decimal values Return: the min of two. Code #1 : Example for min() method # Python Program explaining # min() method # loading decimal library from decima
2 min read
Define and Call Methods in a Python Class
In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, prov
3 min read
Python - Access Parent Class Attribute
A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to
4 min read
Python | Decimal number_class() method
Decimal#number_class() : number_class() is a Decimal class method which returns the indication of the class of Decimal value. Syntax: Decimal.number_class() Parameter: Decimal values Return: indication of the class of Decimal value. Code #1 : Example for number_class() method # Python Program explai
2 min read
call() decorator in Python
Python Decorators are important features of the language that allow a programmer to modify the behavior of a class. These features are added functionally to the existing code. This is a type of metaprogramming when the program is modified at compile time. The decorators can be used to inject modifie
3 min read
Is __init__() a private method in Python?
Here in this article we are going to find out whether __init__() in Python is actually private or not. So we might come across many questions like What actually is __init__() method?What actually are private methods?And, if __init__() is private then how can we access it outside of a class? We have
3 min read