Super() Function in python

Parent class members are by default available to the child class, in the child class, we can access the parent class members directly.

If the parent class and child class contains the method with the same name and if we try to access the parent class method in the child class by using the self then, python will throw a recursion error so to overcome from this naming conflict, python provides the function called super() function.

By using the super() function, we can access the parent class members from the child class.

The super() function is a built-in function, which is useful to call the superclass constructor, methods, and variables explicitly from the child class.

Example 1: If we try to access the parent class members from child class then we will get recursion error

class Parent:
    def method1(self):
        print("Parent Method")
        
class Child(Parent):
    def method1(self):
        self.method1()
        print("Child Method")
        
c=Child()
c.method1()        

The output is:

recursion-error

Example 2: To overcome recursion error we can use the super() function

class Parent:
    def method1(self):
        print("Parent Method")
        
class Child(Parent):
    def method1(self):
        super().method1()  ##calling parent method
        print("Child Method")
        
c=Child()  ##Calling Child method
c.method1()    

The output is:

accessing-parent-method-from-child-class

Example 3: The following demonstrates the parent and child class

class Parent:
    a=10
    def __init__(self):
        print("Parent constructor")
        
    def method1(self):
        print("Parent Instance Method")
        
    @classmethod
    def method2(cls):
         print("Parent Class Method")
         
    @staticmethod
    def method3():
         print("Parent Static Method")
                  
class Child(Parent):  ##child class constructor will execute
    def __init__(self):
        print("Child Constructor")
        
    def method1(self):
        print(super().a)       ##super class variables will execute
        super().method1()
        super().method2()
        super().method3()
        super().__init__()
        
c=Child()  ##creating child object
c.method1() ##calling method1 of parent class by using child class reference

The output is:

super-class-example-three

In the above example, method1, method2, and method3 are present only in the parent class and not in the child class, and there are no naming conflicts; hence, instead of the super() function, we can access those methods by using self only.

But the Constructor is present in both Parent and child class, there is a naming conflict, and hence, we can access the parent class constructor by using the super() function.

class Parent:
    a=10
    def __init__(self):
        print("Parent constructor")
        
    def method1(self):
        print("Parent Instance Method")
        
    @classmethod
    def method2(cls):
         print("Parent Class Method")
         
    @staticmethod
    def method3():
         print("Parent Static Method")
                  
class Child(Parent):
    def __init__(self):
        print("Child Constructor")
        
    def method4(self):
        print(self.a)
        self.method1()
        self.method2()
        self.method3()
        super().__init__()  ##calling parent class constructor by using super() function
        
c=Child()
c.method4()

The output is:

super-class-example-thri-withput-super-class

Example 4: The following example demonstrates the use of the super() function.

class Person:
    def __init__(self,name,age,height,weight):
        self.name=name
        self.age=age
        self.height=height
        self.weight=weight
        
    def display(self):
        print("Name:",self.name)
        print("Age:",self.age)
        print("Hieght:",self.height)
        print("Weight:",self.weight)
        
        
class Student(Person):
    def __init__(self,name,age,height,weight,rollno,marks):
        self.name=name
        self.age=age
        self.height=height
        self.weight=weight
        self.rollno=rollno
        self.marks=marks
        
    def display(self):
        print("Name:",self.name)
        print("Age:",self.age)
        print("Hieght:",self.height)
        print("Weight:",self.weight)
        print("Rollno:",self.rollno)
        print("Marks:",self.marks)
        
s=Student("Revati",24,5.6,50,46,98)
s.display() 

The output is:

using-super-function-example-four-output

In the above example, parent and child class both contain the name, age, height, and weight variables, but when we call variables by using self, then automatically it calls child class constructor.

name, age, height, and weight from the parent class constructor into the child class constructor.

class Person:
    def __init__(self,name,age,height,weight):
        self.name=name
        self.age=age
        self.height=height
        self.weight=weight
        
    def display(self):
        print("Name:",self.name)
        print("Age:",self.age)
        print("Hieght:",self.height)
        print("Weight:",self.weight)
        
        
class Student(Person):
    def __init__(self,name,age,height,weight,rollno,marks):
/*calling the parent class constructor from the child class constructor by using the super() function*/
        super().__init__(name,age,height,weight)  
        self.rollno=rollno
        self.marks=marks
        
    def display(self):
        print("Name:",self.name)
        print("Age:",self.age)
        print("Hieght:",self.height)
        print("Weight:",self.weight)
        print("Rollno:",self.rollno)
        print("Marks:",self.marks)
        
s=Student("Revati",24,5.6,50,46,98)
s.display()        

The output is:

using-super-function-example-four-output-of%20calling-parent-class-constructor

super() function.

class Person:
    def __init__(self,name,age,height,weight):
        self.name=name
        self.age=age
        self.height=height
        self.weight=weight
        
    def display(self):
        print("Name:",self.name)
        print("Age:",self.age)
        print("Hieght:",self.height)
        print("Weight:",self.weight)
        
        
class Student(Person):
    def __init__(self,name,age,height,weight,rollno,marks):
        super().__init__(name,age,height,weight)
        self.rollno=rollno
        self.marks=marks
        
    def display(self):
        super().display()   ##calling parent class display method by using super() function
        print("Rollno:",self.rollno)
        print("Marks:",self.marks)
        
s=Student("Revati",24,5.6,50,46,98)
s.display()        

The output is:

using-super-function-example-four-output-of%20calling-parent-class-display-method

Constructor Overloading

How to Call a Method of Particular Super Function

By using the super() function, we can call the immediate parent class method, and if we want to access the particular superclass method, then we have to use the following two approaches.

1.classname.methodname(self)

A.method1(self) ## This will call the method1 of class A

2.super(classname,self).method1

super(D,self).method1 ##This will call method1() of super class of D

Example: The following example demonstrates how to call the method of a particular function

class A:
    def method1(self):
        print("A Class Method")
        
class B(A):
    def method1(self):
        print("B Class Method")

class C(B):
    def method1(self):
        print("C Class Method")
        
class D(C):
    def method1(self):
        print("D Class Method")
        
class E(D):
    def method1(self):
        B.method1(self)    
        
e=E()
e.method1()        

The output is:

calling-the-method-of-particular-class

class B method

class A:
    def method1(self):
        print("A Class Method")
        
class B(A):
    def method1(self):
        print("B Class Method")

class C(B):
    def method1(self):
        print("C Class Method")
        
class D(C):
    def method1(self):
        print("D Class Method")
        
class E(D):
    def method1(self):
        super(C,self).method1()    ##calling B class method by uisng the second approach
        
e=E()
e.method1()

The output is:

calling-the-method-of-particular-class-by-uisng-second-appraoch

Difference Between Super() Function and Parent Class Instance Variable

We Cannot access the parent class instance variable from child class by using the super() function; we should use the self only.

We can access the parent class instance variable by using the self variable

class Parent:
    a=888
    def __init__(self):
        self.b=999
        
class Child(Parent):
    def method1(self):
        print(self.a)
        print(self.b)

c=Child()
c.method1()  

The output:

accessing-parent-clas-instance-variable-by-using-self

If we try to access the parent class instance variable by using the super() function then, python will throw an error.

But we can access the parent class static variable by using the super() function

class Parent:
    a=888
    def __init__(self):
        self.b=999
        
class Child(Parent):
    def method1(self):
        print(super().a)
        print(super().b)

c=Child()
c.method1()  

The output is:

accessing-parent-class-static-variable-by-using-super

If the parent class and child class are having the instance variable with the same name, then we cannot access because the child object contains only one copy of the instance variable, and only the child class instance variable will execute, and the parent class instance variable won't come in the picture.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions