The super() Constructor Call
What you'll learn: How to use super() to explicitly invoke a parent class constructor from a subclass constructor.
What Is super() in Constructor Context?
You've learned that every subclass constructor implicitly calls super() if you don't write it yourself. Now you'll learn how to explicitly call super() with arguments to invoke a specific superclass constructor.
When your superclass has parameterized constructors, you often need to pass values up to initialize inherited fields properly. The super() call lets you choose which parent constructor to invoke and what arguments to send.
Syntax and Rules
class Animal {
String name;
Animal(String name) {
this.name = name;
}
}
class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name); // Calls Animal(String name)
this.breed = breed;
}
}
Critical rule: super() must be the first statement in your subclass constructor, just like this().
Why Use super() Explicitly?
Think of it like filling out a form with sections. The superclass handles the "general info" section, and the subclass adds the "specific details" section. You pass the general info up with super(), then handle your specific fields afterward.
Without explicit super(), you rely on the no-argument parent constructor — which might not exist or might not properly initialize inherited state.
Dog myDog = new Dog("Buddy", "Golden Retriever");
// super("Buddy") initializes name in Animal
// this.breed = "Golden Retriever" completes Dog initialization
Key Takeaway: Use super() as the first statement in a subclass constructor to explicitly invoke a parameterized superclass constructor and properly initialize inherited fields.