How to Use Java’s super Keyword to Call a Superclass Constructor
This tutorial explains how the super keyword invokes a superclass constructor—both no‑argument and parameterized—illustrates constructor chaining across inheritance hierarchies with concrete Java code examples, and highlights compilation errors when a matching superclass constructor is missing.
In Java, constructors are not inherited, so a subclass must explicitly invoke a superclass constructor using the super keyword, and the call must appear as the first statement in the subclass constructor.
Calling a No‑Argument Superclass Constructor
The syntax super(); triggers the superclass's no‑argument constructor. Example:
class Father {
Father(){
System.out.println("Here is the constructor of class Father.");
}
}
class Son extends Father {
Son(){
super();
System.out.println("Here is the constructor of class Son.");
}
}
public class SonTest {
public static void main(String[] args){
Son son = new Son();
}
}Running the program prints:
Here is the constructor of class Father.
Here is the constructor of class Son.If the explicit super(); line is removed, the Java compiler automatically inserts a call to the superclass's no‑argument constructor, so the output remains unchanged. However, if the superclass lacks a no‑argument constructor, compilation fails. For instance, changing Father to have only a constructor with an int parameter produces the error "cannot find symbol: constructor Father()".
Calling a Parameterized Superclass Constructor
To initialize superclass fields with specific values, use super(arg1, arg2, ...);. Example with a Car base class and a Bus subclass:
public class Car {
private String brand;
private String color;
private double speed;
public Car(String brand, String color, double speed){
this.brand = brand;
this.color = color;
this.speed = speed;
}
// getters omitted for brevity
}
public class Bus extends Car {
public int scale;
public Bus(int scale, String brand, String color, double speed){
super(brand, color, speed);
this.scale = scale;
}
}Here the Bus constructor forwards the brand, color, and speed to the Car constructor via super(...), then initializes its own field scale.
Constructor Chain
When a subclass constructor invokes its superclass constructor—explicitly or implicitly—the superclass constructor may in turn invoke its own superclass constructor, forming a complete constructor chain that ends at Object, whose constructor does not call any further constructor.
Example demonstrating the chain:
class GrandFather {
GrandFather(){
System.out.print("GrandFather constructor
");
}
}
class Father extends GrandFather {
Father(){
System.out.print("Father constructor
");
}
}
class Son extends Father {
Son(){
System.out.print("Son constructor
");
}
}
public class SonTest {
public static void main(String[] args){
Son son = new Son();
}
}Running this program prints:
GrandFather constructor
Father constructor
Son constructorThe output shows that constructors are executed from the top of the inheritance hierarchy down to the most derived class. In real projects with deep inheritance trees, developers should be aware of this order.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
