Java ProgramsOOPsuper Keyword

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. 1Car(String name) { super(name); } calls Vehicle's constructor to set up the inherited name field, rather than assigning it directly in Car.
  2. 2Car overrides describe(), but its first line is super.describe();, which runs Vehicle's original version of the method.
  3. 3After that call returns, Car's own describe() continues, printing its own additional line.
  4. 4Without super(name), Car would have no way to initialize the name field that Vehicle declares, since Car doesn'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

Related Programs