The super Keyword
What Does super Refer To?
Inside a subclass, super is a reference to the parent (superclass) part of the current object. It has three main uses: calling an overridden method's parent implementation, calling a specific parent constructor, and accessing a parent field that has been shadowed by a field of the same name in the subclass.
Calling the Parent's Version of a Method
When a subclass overrides a method, the parent's implementation isn't lost — it's just hidden by the override. You can still reach it with super.methodName(...), which is useful when the subclass wants to extend the parent's behavior rather than fully replace it.
Extending, rather than replacing, a parent method
public class Employee {
void describe() {
System.out.println("An employee of the company");
}
}
public class Manager extends Employee {
@Override
void describe() {
super.describe(); // run the parent's version first
System.out.println("...who also manages a team");
}
}
public class Main {
public static void main(String[] args) {
Manager m = new Manager();
m.describe();
}
}An employee of the company ...who also manages a team
Calling a Specific Parent Constructor
Use super(...) to explicitly call one of the parent class's constructors, passing whatever arguments that constructor needs. This lets the subclass ensure the inherited part of the object is initialized correctly before the subclass's own constructor body runs.
Explicitly choosing a parent constructor
public class Vehicle {
String make;
Vehicle(String make) {
this.make = make;
System.out.println("Vehicle constructed: " + make);
}
}
public class Car extends Vehicle {
int doors;
Car(String make, int doors) {
super(make); // must be the first statement
this.doors = doors;
System.out.println("Car constructed with " + doors + " doors");
}
public static void main(String[] args) {
Car myCar = new Car("Toyota", 4);
}
}Vehicle constructed: Toyota Car constructed with 4 doors
Accessing a Shadowed Parent Field
If a subclass declares a field with the same name as one in its parent, the subclass's field shadows the parent's. This is rare and generally best avoided, but when it happens, super.field lets you reach the parent's version explicitly.
Reaching a shadowed parent field with super
public class Base {
String label = "base label";
}
public class Derived extends Base {
String label = "derived label"; // shadows Base's label
void printLabels() {
System.out.println(label); // "derived label"
System.out.println(super.label); // "base label"
}
}derived label base label
Putting It All Together
A worked example combining super(), super.method(), and super.field
public class Shape {
String name = "shape";
Shape(String name) {
this.name = name;
}
void describe() {
System.out.println("This is a " + name);
}
}
public class Circle extends Shape {
double radius;
Circle(double radius) {
super("circle"); // calls Shape's constructor
this.radius = radius;
}
@Override
void describe() {
super.describe(); // calls Shape's describe()
System.out.println("with radius " + radius);
}
public static void main(String[] args) {
Circle c = new Circle(5.0);
c.describe();
}
}This is a circle with radius 5.0
super.method() reaches an overridden method's parent implementation
super(...) calls a specific parent constructor, and must be the first statement
super.field reaches a parent field that has been shadowed by a same-named subclass field