skip to content

What is constructor chaining in Java, and how do this() and super() participate in it?

level: juniorimportance: must knowfreq 70%

answer

  1. this() = same class, super() = parent class
  2. Must be the FIRST statement, at most one
  3. Compiler auto-inserts super() if you write neither
  4. Funnel convenience constructors into one primary constructor
  5. Parent is fully constructed before the child body runs

basics

~10 s

Constructor chaining means one constructor calls another so you don't repeat setup. this(...) calls another constructor in the same class; super(...) calls the parent class's constructor.

solid answer

~40 s

Constructor chaining is when one constructor delegates to another to centralize initialization logic. Within the same class you call this(args), which routes to a matching overloaded constructor; up the inheritance hierarchy you call super(args), which invokes the chosen parent constructor. Either call, if present, must be the very first statement in the constructor body, and you can use at most one of them. A common pattern: short convenience constructors call a single 'primary' constructor via this(...) with default values, so validation and field assignment live in exactly one place. super(...) ensures the parent's portion of the object is fully initialized before the subclass's own body runs. This keeps initialization DRY, avoids duplicated and divergent setup, and guarantees the parent is constructed first.

code

java · 14 lines
java
class Rectangle {
    private final int width, height;

    Rectangle(int width, int height) { // primary
        this.width = width;
        this.height = height;
    }
    Rectangle(int side) {              // square convenience ctor
        this(side, side);              // chains to the primary ctor
    }
    Rectangle() {                      // default convenience ctor
        this(1);                       // -> this(1,1)
    }
}

go deeper

for a junior

Knows this() targets the same class and super() the parent, and that one funnels convenience constructors into a primary one.

for a middle

Explains the first-statement rule, that at most one of this()/super() is allowed, and the implicit super() insertion.

for a senior

Designs a single primary constructor that all others chain into, places validation there, and reasons about construction order across a hierarchy.

for a principal

Weighs constructor chaining against static factories/builders for many-parameter or optional-arg APIs, and sets team conventions for canonical constructors and immutability.

## What a constructor is A **constructor** is a special block of code that runs when you create an object with `new ClassName(...)`. Its job is to put a brand-new object into a valid initial state (assign fields, validate inputs). It has the same name as the class and no return type. A class can have **several constructors** with different parameter lists (this is *constructor overloading*). For example, one might take all fields, another take fewer and fill in defaults. ## The problem chaining solves If each constructor independently assigns fields and validates, you duplicate that logic. Duplication drifts: you fix a bug in one constructor and forget another. **Constructor chaining** lets one constructor *delegate* to another so the real work lives in one place. There are two directions of delegation: - **`this(args)`** — calls *another constructor of the same class*. The compiler picks the overloaded constructor whose parameters match `args`. - **`super(args)`** — calls a *constructor of the direct parent class* (the class named after `extends`). Every Java class except `java.lang.Object` has a parent. ## The first-statement rule If you write `this(...)` or `super(...)`, it **must be the first statement** in the constructor body, and you may use **at most one** of them (you can't call both, because a constructor can only delegate once). Why first? The language guarantees that before any of *your* code in a constructor runs, the object's superclass part is already initialized. Allowing statements before the delegation could read fields that aren't set up yet. ## Implicit super() If you do *not* write `this(...)` or `super(...)` as the first statement, the compiler **automatically inserts a no-argument `super()`** call. This means: even a constructor that looks like it does nothing special still calls the parent's no-arg constructor first. (If the parent has no accessible no-arg constructor, this auto-insertion fails to compile — you must call `super(...)` explicitly with the right arguments.) ## A worked example ```java class Person { private final String name; private final int age; Person(String name, int age) { // primary constructor if (age < 0) throw new IllegalArgumentException("age"); this.name = name; this.age = age; } Person(String name) { // convenience constructor this(name, 0); // chains via this(...) to the primary one } } class Employee extends Person { private final String dept; Employee(String name, int age, String dept) { super(name, age); // chains up to Person's constructor this.dept = dept; } } ``` Creating `new Employee("Ann", 30, "Eng")` runs, in order: `Person(String,int)` first (because of `super(...)`), then the `Employee` body. The `Person(String)` convenience form reuses the validation in the primary constructor instead of copying it. ## Why it matters Chaining keeps initialization **DRY** (one source of truth), guarantees **construction order** (parent before child), and makes **convenience constructors** thin wrappers over a single canonical one — the standard, idiomatic Java way to build objects with optional/default parameters.

  • Can you call both this() and super() in the same constructor?
    No. A constructor may contain at most one explicit constructor-invocation, and it must be the first statement. Calling this() delegates to another constructor in the same class, which will eventually reach super() itself, so calling both would invoke the parent twice.
  • What happens if a constructor has neither this() nor super()?
    The compiler inserts an implicit no-arg super() as the first statement, invoking the direct parent's no-arg constructor.

Like filling out a form: super() means a colleague completes the shared cover page before you add your section; this() means handing the form to a more detailed version of yourself who already knows how to fill it in.

saying these in an interview costs you the question

  • Claiming you can put statements before this()/super()
  • Saying you can call both this() and super() together
  • Thinking the child constructor runs before the parent's
  • Believing chaining is optional plumbing rather than the standard way to avoid duplicated init

context