Implicit super() Call in Constructors
What you'll learn: You'll discover that Java automatically calls the superclass's no-argument constructor for you, even when you don't write it explicitly.
The Hidden Constructor Call
When you create a subclass, Java always ensures the superclass is initialized first. If you don't explicitly call a superclass constructor using super(), Java quietly inserts super() as the very first statement in your constructor.
This happens behind the scenes—you never see it in your code, but it's there.
Why Java Does This
Think of inheritance like building a house on a foundation. Before you can add your custom rooms (subclass features), the foundation (superclass) must be properly built. Java enforces this by making sure the superclass constructor runs first, automatically.
What This Means in Practice
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
// Java secretly adds: super();
System.out.println("Dog constructor");
}
}
When you create a Dog, both constructors run:
Animal constructor
Dog constructor
Even though you didn't write super() in the Dog constructor, Java inserted it for you.
When This Matters
This automatic behavior only works if the superclass has a no-argument constructor. If the superclass only has parameterized constructors, you must explicitly call one using super(arguments), or your code won't compile.
class Dog extends Animal {
Dog() {
// This is what Java does automatically
super();
System.out.println("Dog constructor");
}
}
Key Takeaway: Java automatically inserts super() as the first statement in every subclass constructor unless you explicitly call a superclass constructor yourself—ensuring the inheritance chain is always properly initialized from the top down.