What is the super() function?
The use of super() comes into play when we implement inheritance. It is used in a child class to refer to the parent class without explicitly naming it. It makes the code more manageable, and there is no need to know the name of the parent class to access its attributes.
Note: Make sure to add parenthesis at the end to avoid a compilation error.
Use cases of the super() function
The super function is used in three relevant contexts:
Accessing parent class properties
Consider the fields named fuelCap defined inside a Vehicle class to keep track of the fuel capacity of a vehicle. Another class named Car extends from this Vehicle class. We declare a class property inside the Car class with the same name, i.e., fuelCap, but with a different value. Now, if we want to refer to the fuelCap field of the parent class inside the child class, we will have to use the super() function.
Let’s understand this using the code below:
xxxxxxxxxx
class Vehicle: # defining the parent class
fuelCap = 90
class Car(Vehicle): # defining the child class
fuelCap = 50
def display(self):
# accessing fuelCap from the Vehicle class using super()
print("Fuel cap from the Vehicle Class:", super().fuelCap)
# accessing fuelCap from the Car class using self
print("Fuel cap from the Car Class:", self.fuelCap)
obj1 = Car() # creating a car object
obj1.display() # calling the Car class method display()