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: