Python Super function
The super function is used to access and call the methods or functions involved in the parent class during Inheritance.
What is Inheritance?
The process where the parent class inherits some of its members, including methods, variables, etc., to the child class or subclass is known as Inheritance.
Condition for implementing super function:
The super function should contain the same arguments as the function that is called by the object.
Uses of super function:
- We need not specify the methods and members present within the parent class while calling them from the child class / sub-class.
- Supports code optimization and reusability as there is no need to rewrite the functions that are being called.
- Working with Multiple Inheritance can proceed with the help of the super() function. So, it obeys Multiple Inheritance.
The super function can be implemented in:
- Single Inheritance
- Multiple Inheritance
Syntax / General usage of Super function:
super().__init__()
Single Inheritance using Super function:
If the child classes inherit members only from a single parent class, then that inheritance is called Single Inheritance. It consists of a Child class that inherits members from a Parent class.
We shall discuss the implementation of the super function in Single Inheritance with an example.
Example:
Let’s consider an example of vehicles. Cycles, Bikes, Cars, etc., come under the category of vehicles. They share a common point, i.e., we can ride all of them, and they make transportation easier.
So, we can conclude that these classes are the child classes of Vehicles (a parent class for all those classes). For example, let's frame a program using a super function and find the output.
Parent class: Vehicles
Child classes: Cycles, Bikes, Cars.
Multiple Inheritance using Super function:
If the child classes inherit members from more than one parent class, then that inheritance is called Multiple Inheritance. It consists of Child class which inherits members from two parents i.e., Father class and Mother class.
We shall discuss the implementation of the super function in Multiple Inheritance with an example.
Example:
Let’s consider an example of Animals. Mammals, Non-winged Mammals, Non-marine Mammals, etc., also come under the category of Animas. They share a common point, i.e., they are Animals. Non-winged Mammals, Non-marine Mammals also have a common point, i.e., they belong to the mammals' category.
So, we can conclude that these classes are the child classes of Animals (which is a parent class for all those classes) and Non-winged Mammals, and Non-marine Mammals classes are the child classes of Mammals class. Again, being a mammal, “Non-winged Mammals” and “Non-marine animals” are a part of “Animals”. Now, let a class Dog be taken. It is a combination of Non-winged mammals and Non-marine mammals. So, the dog undergoes Multiple Inheritance.
Parent class: Animals.
Child class: Mammals.
Grandchild classes: Non-winged Mammals, Non-marine Mammals
Great grandchild class: Dog(Non-winged Mammals, Non-marine Mammals)
Here, the father class is the " Non-winged Mammals “ class, whereas the mother class is the " Non-marine Mammals “ class.
Let's consider an example program which describes a super function.
Program:
class vehicles:
def __init__(self,w,p):
self.wheels = w
self.passenger = p
def wheelNo(self):
print("The number of wheels are ")
print(self.wheels)
def passengerNo(self):
print("The maximum number of passengers are ")
print(self.passenger)
class Bikes(Vehicles):
def __init__(self,w,p):
print("Bikes: ")
super().__init__(w,p)
class Cycles(Vehicles):
def __init__(self,w,p):
print("Cycles: ")
super().__init__(w,p)
class Cars(Vehicles):
def __init__(self,w,p):
print("Cars: ")
super().__init__(w,p)
# objects being created in child classes
cy1 = Cycles(2,2)
cy1.wheelNo()
cy1.passengerNo()
b1 = Bikes(2,2)
b1.wheelNo()
b1.passengerNo()
ca1 = Cars(4,5)
ca1.wheelNo()
ca1.passengerNo()
Output:
Cycles:
The number of wheels are
2
The maximum number of passengers are
2
Bikes:
The number of wheels are
2
The maximum number of passengers are
2
Cars:
The number of wheels are
4
The maximum number of passengers are
5
Explanation:
The classes " Bikes ", " Cycles " and " Cars " are inherited from the class " Vehicles ". So, " Vehicles " is the parent class whereas the classes Bike, Cycles and Cars are the child classes of Vehicle. The methods that define the number of wheels and passengers are declared in the parent class " Vehicles ". But, we have created objects in the child class. Although, we can access those methods when we use the super function. As the super function is used in all three child classes, the statements present in the parent class methods have been printed.