In any object-oriented programming language, inheritance plays a very important role in reusing the code for multiple similar tasks. Versioning a software or patching a software often requires inheritance and the super().
There are many cases where we want to reuse the code to create the updated version of an application. With the help of inheritance, a base class can inherit the properties and methods of the parent class.
The Super() method refers to the parent class in the child class. It provides a temporary object for the parent class. This temporary object can be used for accessing the parent class method. It follows the property of code reusability.
Use of super() method:
Super methods can be used in many scenarios. Some of them are.
- Super() with single Inheritance
- Super() with Multiple Inheritance
When a base class is derived from a single parent class then it is a case of single inheritance. When a base class is derived from two or more parent classes is known as multiple inheritances.
Super() with single Inheritance:
With super() method init method of a base class can be derived into the parent class. The Super() method is a temporary object of the base class.
class base(): def __init__(self): print("In the base class") self.s1 = "base class vara" class derived(base): def __init__(self): print("In the derived class") self.s2= "derived class var" super().__init__() a=derived() print(a.s1) print(a.s2)
Here, first defines the base class with an init method which first prints and then initiates a variable s1. Secondly, we define a derived class with the init method which first prints and then initialized a variable.
Then, it calls the base class with the super method. At last print the values of s1 and s2 with the object of the class.
Super() with Multiple Inheritance:
With the super() method init methods of the base classes can be derived into the parent class. The Super() method gives a temporary object of the base class. In case of multiple inheritances init method of derived classes calls according to MRO.
Multiple methods of different classes with the same name can be called from the base class with the super method. Bypassing the reference to the classes as in MRO to skip the search for the init method in that class by the interpreter.
class base1(): def __init__(self): print("In the base1 class") self.s1= "base1 class var" class base2(): def __init__(self): print("In the base2 class") self.s2= "base2 class var" class derived(base1,base2): def __init__(self): print("In the derived class") super().__init__() # calls base class 1 super(base1,self).__init__() # calls base class 2 a = derived()
Here, first we have defined two base classes with their init methods and variables. Then, define a derived class that inherits both the base class. In the derived class first, call the init method of the base class with the super method.
Secondly, call the base2 class by passing the reference to the first class and derived class in the super method.
Method Resolution Order (MRO):
Method Resolution Order is the order in which methods are inherited in the derived class. The order of inheritance of class can be checked with the __mro__. The interpreter first checks the attributes in the derived class then the base class as per mro
class base1: def _init_(self) print("In the base class") self.s1 = " base1 class var" class base2: def _init_(self): print("In the base2 class") self.s2 ="base2 class var" class derived(base1, base2): def_init_(self): print(In derived class") super()._init_() super(base1,self)._init_() print(derived._mro_)
We hope this tutorial has given a clear idea about Python super(), super() with init, how to use super() with single inheritance, super() with multiple inheritance, and Method Resolution Order (MRO). We can achieve code reusability using super().
In an inherited subclass, we can refer to a parent class with the help of the super() function. super() is the most efficient way to refer to elements of parent class from sub class. In real-life, companies use it while updating a functionality of previous version of a software to a newer version.
For example, updating from Windows 10 to 11 might require this super() if Windows 11 uses the functionalities of Windows 10 implicitly and not that of Windows 11.