Method and Constructor Overriding Super in Python

In this class, we discuss Method and Constructor Overriding Super in Python.

For Complete YouTube Video: Click Here

Method and Constructor Overriding

The reader should have prior knowledge of inheritance, method, and constructor overloading. Click here.

Take an example and understand the concept of method and constructor overriding.

class parent:
    def f(self):
        print("the f method in parent class")
class child(parent):
    def f(self):
        print("the f method in child class")
        
ob1 = child()
ob1.f()

In the program above, we have a class parent. And in the parent class, we have a method f.

We have a class child. And the child class inherits the parent class.

In the child class, we have a method f.

Both class parent and class child have the same method name f.

We have the same method names in the base and inheritance classes. We call method overriding.

Writing constructor in base and inherited class we call constructor overriding.

We created an object for the child class. And called the method f.

The child class method f will be called.

If we want to call the base class method. we use super() function.

Super() Function

We use the super() function to call the base class method.

An example is given below.

# Super() function to call parent class methods
class parent:
    def f(self):
        print("the f method in parent class")
class child(parent):
    def f(self):
        super().f()
        print("the f method in child class")
        
ob1 = child()
ob1.f()

Output:
the f method in parent class
the f method in child class

Constructor Overriding Example

# constructor overloading
class parent:
    def __init__(self,k):
        self.instvar=k
    def f(self):
        print("this f method displays parent class instance variable",self.instvar)

class child(parent):
    def __init__(self, l):
        self.instvar1=l
    def f1(self):
        print("this f1 method displays child class instance variable",self.instvar1)
        
ob1=child(20)
ob1.f1()

In the above example, we are having a constructor in base and inherited class.

Inherited class is creating a new instance variable instvar1.

We created an object for the child class. When the object is created, it will call child class constructor.

If we want to call the base class constructor and initialize the variable instvar. we use super() function.

The example is shown below.

class parent:
    def __init__(self,k):
        self.instvar=k
    def f(self):
        print("this f method displays parent class instance variable",self.instvar)

class child(parent):
    def __init__(self,k,l):
        super().__init__(k)
        self.instvar1=l
    def f1(self):
        print("this f1 method displays child class instance variable",self.instvar1)
        
ob1=child(10,20)
ob1.f1()
ob1.f()

Output:
this f1 method displays child class instance variable 20
this f method displays parent class instance variable 10

The inherited class constructor takes two parameters in the program above, and one parameter is sent to the base class constructor using the super() function.

Multiple inheritance examples

Take an example and understand the multiple inheritance concept in depth.

Suppose we have a class c1, and we have class c2 and c3.

Class c2 is inheriting class c1. and class c3 is inheriting class c1.

Take one more class, c4. Class c4 is inheriting both class c2 and class c3.

If the classes c1, c2, and c3 have the function f.

Which class function will be called?

The example is given below.

# Multiple inheritance examples
class c1:
    def f(self):
        print("from class1")
class c2(c1):
    def f(self):
        print("from class2")
class c3(c1):
    def f(self):
        print("from class3")
class c4(c2,c3):
    def f1(self):
        print("from class4 f1 method")
ob1=c4()
ob1.f()

Output:
from class2

In the above program, we created an object to class c4. ob1 is referencing to object of class c4.

We called the function f using the object ob1.

Which f function will be called?

Output: from class2.

Function f is called from class c2.

During the class definition. we defined class c4(c2,c3).

c2 is given first in the class definition.

The python interpreter will check for function f in classes from left to right.

Take one more example for a better understanding.

Analyze the program given below and identify the output displayed by the code.

class c1:
    def f(self):
        print("from class1")
class c2(c1):
    def f(self):
        print("from class2")
class c3(c1):
    pass
class c4(c3,c2):
    def f1(self):
        print("from class4 f1 method")
ob1=c4()
ob1.f()

Output:
from class2
class c1:
    def f(self):
        print("from class1")
class c2(c1):
    def f(self):
        print("from class2")
class c3(c1):
    def f(self):
        print("from class3")
class c4(c3,c2):
    def f(self):
        print("from class4 f method")
ob1=c4()
ob1.f()
c1.f(ob1)# calling function using class name
c2.f(ob1)
c3.f(ob1)

Output:
from class4 f method
from class1
from class2
from class3

Another way of calling functions using class name.

class c1:
    def f(self):
        print("from class1")
class c2(c1):
    def f(self):
        print("from class2")
class c3(c1):
    def f(self):
        print("from class3")
class c4(c3,c2):
    def f(self):
        print("from class4 f method")
        c1.f(self)
        c2.f(self)
        c3.f(self)
ob1=c4()
ob1.f()

Output:
from class4 f method
from class1
from class2
from class3