Inheritance and constructors (super() call)
When an object of a subclass is created, the constructors of all superclasses are called in order from the top of the hierarchy down to the subclass.
Constructor chaining rules:
- The first statement of every constructor must be a call to
super(...)orthis(...)(another constructor in the same class). - If no explicit call is present, the compiler inserts
super()(no‑argument constructor of the parent). - If the parent class does not have a no‑argument constructor, you must explicitly call a parameterized
super(...).
Example:
class GrandParent { GrandParent() { System.out.println("GrandParent constructor"); }}
class Parent extends GrandParent { Parent(String name) { // implicit super() calls GrandParent() System.out.println("Parent constructor: " + name); }}
class Child extends Parent { Child() { super("ChildName"); // explicit call to Parent(String) System.out.println("Child constructor"); }}Output when new Child();:
GrandParent constructorParent constructor: ChildNameChild constructorImportant: If a class defines any constructor (even parameterized), the default no‑argument constructor is not automatically provided. So if you extend such a class, you must explicitly call an existing constructor.
class Base { Base(int x) { }}class Derived extends Base { Derived() { super(10); // required }}Order of field initialization:
- Superclass static initializers and static fields
- Subclass static initializers and static fields
- Superclass instance initializers and instance fields
- Superclass constructor
- Subclass instance initializers and instance fields
- Subclass constructor