super keyword in Java
The ‘super’ keyword in Java is used to refer to the superclass (parent class) of the current object. It is primarily used in three contexts: to call the superclass’s constructor, to call the superclass’s methods, and to access the superclass’s fields.

Table of Contents
Uses of ‘super’ Keyword
- Calling Superclass Constructor: The ‘super()’ call is used to invoke the superclass’s constructor. This must be the first statement in the subclass constructor.
- Accessing Superclass Methods: The ‘super’ keyword can be used to call methods defined in the superclass.
- Accessing Superclass Fields: The ‘super’ keyword can be used to access fields of the superclass if they are not hidden by fields in the subclass.
Example
Let’s consider a superclass Vehicle and a subclass Car.
Syntax
class Vehicle {
String brand = "Ford";
Vehicle() {
System.out.println("Vehicle constructor called");
}
void honk() {
System.out.println("Vehicle honks");
}
}
class Car extends Vehicle {
String model = "Mustang";
Car() {
super(); // Calling superclass constructor
System.out.println("Car constructor called");
}
void display() {
System.out.println("Brand: " + super.brand); // Accessing superclass field
super.honk(); // Calling superclass method
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.display();
}
}
Explanation of the Example
- Superclass Constructor Call: In the ‘Car’ constructor, ‘super()’ is used to call the ‘Vehicle’ constructor.
- Superclass Field Access: The ‘display’ method in the ‘Car’ class uses ‘super.brand’ to access the ‘brand’ field of the ‘Vehicle’ class.
- Superclass Method Call: The ‘display’ method in the ‘Car’ class uses ‘super.honk()’ to call the ‘honk’ method of the ‘Vehicle’ class.
Key Points
- Constructor Call: ‘super()’ must be the first statement in the subclass constructor to call the superclass constructor.
- Field and Method Access: ‘super’ is used to differentiate between superclass fields/methods and those in the subclass.
Benefits
- Code Reusability: Promotes reuse of the superclass’s code, reducing redundancy.
- Clarity: Enhances code readability by clearly indicating the use of superclass members.
The ‘super’ keyword is essential for managing inheritance hierarchies in Java, ensuring that the subclass can properly utilize and extend the functionality of its superclass.