What is constructor chaining, and how do this() and super() work?

7 minintermediateconstructorsthissuper

Quick Answer

Constructor chaining is one constructor invoking another to reuse initialization logic. this(...) calls another constructor in the same class; super(...) calls a constructor in the direct superclass. Either must be the first statement in a constructor, so only one of them can be used per constructor, and if neither is written explicitly, the compiler inserts an implicit super() call.

Detailed Answer

Constructor chaining lets one constructor delegate to another instead of duplicating setup logic:

class Employee {
    String name;
    double salary;

    Employee(String name) {
        this(name, 0.0); // chains to the two-arg constructor
    }

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
}

class Manager extends Employee {
    int teamSize;
    Manager(String name, double salary, int teamSize) {
        super(name, salary); // calls Employee's constructor first
        this.teamSize = teamSize;
    }
}

Rules:

  • this(...) calls another constructor in the same class; super(...) calls a constructor in the immediate superclass.
  • Either call, if present, must be the first statement in the constructor body — so you can never call both this(...) and super(...) in the same constructor.
  • If a constructor has neither, the compiler implicitly inserts super() (a no-arg call to the superclass constructor) as the first line — which fails to compile if the superclass has no accessible no-arg constructor.

This guarantees superclass state is always fully initialized before subclass initialization runs, which is why field initializers and instance blocks in the superclass execute before the subclass constructor body.