As we discussed before, you can access the constructor and methods of the parent class in the child class using
This method tends to get confusing when the hierarchy has multiple levels because you have to remember the exact names of the parent classes and where to use them. An easier way to do this is by using the
super function allows the child class (or subclass) to access inherited methods of the parent class (or superclass) that have been overwritten in the child class.
According to the official Python documentation:
Super is used to return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.
super method can be used in multiple scenarios.
We can use
super in single inheritance to refer to the parent (or super) class or multiple classes without explicitly naming them. This is just a shortcut and helps make your code maintainable and easy to understand.
As discussed earlier, it is possible in Python for a class to inherit from multiple parent classes. You can use the
super method for multiple or collaborative inheritance. This can be done only in Python because some languages do not support inheritance from multiple classes.
super()is not limited to use inside methods. You can specify the appropriate references by specifying the arguments of the
In Python 3 and above, the syntax for
Now consider the following code snippet where the
Student class inherits from (or “is a”)