skip to content

In what order do constructors execute when you create an object whose class extends another class? Walk through what happens from the `new` call down to the body of your constructor.

level: juniorimportance: must knowfreq 68%

answer

  1. super() runs first — always, implicit if omitted
  2. Walk up to Object, then bodies unwind top-down
  3. Field initializers run right after super() returns
  4. Parent fully built before child body runs
  5. this(...) or super(...) must be the first statement

basics

~10 s

The parent constructor runs before the child constructor body. So construction goes top-down: the topmost ancestor (Object) is built first, then each subclass in turn, ending with the class you actually created.

solid answer

~40 s

When you write `new Child()`, the Child constructor starts, but its very first action is a call to a superclass constructor (`super(...)`). If you don't write one, the compiler inserts an implicit no-arg `super()`. So control walks all the way up the chain to `Object` before any constructor *body* runs. Then the bodies execute top-down: Object's body, then the grandparent's, the parent's, and finally Child's. Field initializers and instance-initializer blocks of a class run right after its `super(...)` returns and before the rest of that class's constructor body. The net effect: by the time your Child constructor body executes, every ancestor is fully initialized. This guarantees a subclass can safely rely on inherited state the parent set up.

go deeper

for a junior

States the basic rule: parent constructor runs before child constructor body; construction is top-down from Object.

for a middle

Explains the implicit super() insertion and where field initializers/instance blocks fit (after super() returns, before the rest of the body).

for a senior

Traces a multi-level hierarchy precisely, distinguishes this()/super() delegation, and separates static (class-load) from instance (per-object) ordering.

for a principal

Connects the ordering rule to the invariant it protects (parent state ready before child runs) and can reason about why the JLS mandates super-first to keep object initialization sound.

## Setup of terms - A *class* is a blueprint for objects. - *Inheritance* lets one class (the *subclass* or *child*) build on another (the *superclass* or *parent*) using `extends`. - A *constructor* is a special method that runs when you create an object with `new`; its job is to initialize the new object's fields. - Every class in Java ultimately inherits from `java.lang.Object`, the root of all classes. - A *constructor chain* is the sequence of constructors that run, one per class, from the root down to the class you instantiated. ## The core rule A constructor's first action is *always* to run a superclass constructor before doing anything else. You can make this explicit with `super(args)` as the first statement; if you omit it, the compiler silently inserts `super()` (the no-arg parent constructor) as the first statement. (Alternatively the first statement can be `this(...)`, delegating to another constructor of the *same* class, which itself eventually calls `super`.) Because each constructor calls up before running its own body, control travels all the way up to `Object` first. Then the bodies unwind ***top-down***: `Object` finishes first, then each descendant in order, ending with the class you created. ## Where field initializers fit Within a single class, the steps after `super(...)` returns are: - (1) run that class's **instance field initializers** (e.g. `private int x = 5;`) and **instance initializer blocks** (`{ ... }`) in source order, then - (2) run the rest of the constructor body. So for a two-level hierarchy `Base` → `Derived`, creating a `Derived` runs: `Object`'s constructor → `Base`'s `super()` returns, `Base`'s field initializers, `Base`'s constructor body → back in `Derived`, `super()` returns, `Derived`'s field initializers, `Derived`'s constructor body. ## Why this order A subclass typically depends on state established by its parent. Running parents first means that by the time a subclass's body runs, all inherited fields are already set, so the subclass can build on them safely. ## Static vs instance Static initializers and static fields belong to the *class*, not the instance, and run once when the class is first loaded/initialized — that happens before any instance is constructed, and is a separate concern from per-object constructor order. ## Worked trace For classes `A` (extends Object), `B extends A`, `C extends B`, the statement `new C()` produces this body-execution order: `Object` body, `A` body, `B` body, `C` body — i.e. strictly top-down.

  • If a constructor's first statement is this(...) instead of super(...), does the superclass still get constructed?
    Yes. this(...) delegates to another constructor in the same class, and that one (or the chain it triggers) must eventually reach a super(...) call — explicit or compiler-inserted — so the superclass is always constructed exactly once.
  • Can you call both super(...) and this(...) in the same constructor?
    No. Each must be the first statement, and a constructor can have at most one of them, so they're mutually exclusive within a single constructor.

saying these in an interview costs you the question

  • Saying the child constructor runs before the parent — it's the reverse
  • Thinking you must always write super() yourself (the compiler inserts it)
  • Confusing static initializer timing (class load, once) with per-instance constructor order
  • Believing field initializers run before super() — they run after it returns

context