Open In App

Call Parent class method – Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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() 

Output
Geeks for Geeks

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() 

Output
Inside 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() 

Output
Inside 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() 

Output
Inside 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:



Next Article
Article Tags :
Practice Tags :

Similar Reads