super Keyword in Java
beginner· OOP · Inheritance
Problem
The super keyword lets a subclass reach up to its parent class — calling super(...) runs the parent's constructor, and calling super.method() runs the parent's version of a method the subclass has overridden.
Create a Vehicle class and a Car subclass that uses super to call the parent's constructor and its overridden describe method.
Input
new Car("Sedan")
Output
Sedan is a vehicle
Sedan is specifically a car
Java Program
Java
class Vehicle {
String name;
Vehicle(String name) {
this.name = name;
}
void describe() {
System.out.println(name + " is a vehicle");
}
}
class Car extends Vehicle {
Car(String name) {
super(name); // calls Vehicle's constructor
}
@Override
void describe() {
super.describe(); // runs Vehicle's version first
System.out.println(name + " is specifically a car");
}
}
public class SuperKeywordDemo {
public static void main(String[] args) {
Car car = new Car("Sedan");
car.describe();
}
}Output
Sedan is a vehicle
Sedan is specifically a car
Core Logic
Delegating to the parent's constructor and method with super, then adding the subclass's own behavior afterward, reuses the parent's logic instead of duplicating it.
How It Works
- 1
Car(String name) { super(name); }callsVehicle's constructor to set up the inheritednamefield, rather than assigning it directly inCar. - 2
Caroverridesdescribe(), but its first line issuper.describe();, which runsVehicle's original version of the method. - 3After that call returns,
Car's owndescribe()continues, printing its own additional line. - 4Without
super(name),Carwould have no way to initialize thenamefield thatVehicledeclares, sinceCardoesn't declare it itself.
Calling
car.describe() first prints "Sedan is a vehicle" from Vehicle's method via super.describe(), then "Sedan is specifically a car" from Car's own code.💡
Key Point: super.describe() is what lets Car build on top of Vehicle's behavior instead of completely replacing it — the parent's version still runs, just as one step inside the subclass's own version.
Key Concepts
super()super.method()inheritance