skip to content

Constructors

How Java objects get initialized: constructor declarations, chaining with this and super, and the rules that govern construction inside a hierarchy. Interviewers probe here because initialization order bugs are subtle and expensive.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

19

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

open as a page

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

level: juniorimportance: must knowfreq 70%

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.

open as a page

Are constructors inherited in Java? Explain how subclass construction actually works.

level: juniorimportance: must knowfreq 65%

basics

~20 s

No. Constructors are not inherited. A subclass must define its own constructors. When a subclass object is built, its constructor first calls a superclass constructor (with super(...), or an implicit super() if you omit it).

open as a page

What is a constructor in Java, and what is the implicit default constructor?

level: juniorimportance: must knowfreq 80%

basics

~20 s

A constructor is a special method that runs when you create an object with new, to set it up. If you write no constructor, Java adds a hidden no-argument one for you so you can still write new MyClass().

open as a page

When does the Java compiler NOT generate the default no-arg constructor, and what problem can that cause?

level: middleimportance: must knowfreq 78%

basics

~20 s

As soon as you write any constructor of your own — even one that takes parameters — the compiler stops adding the free no-arg one. Then new MyClass() won't compile unless you also write the no-arg constructor yourself.

open as a page

Within a single class, when do instance field initializers and instance initializer blocks run relative to super(...) and the rest of the constructor body?

level: middleimportance: should knowfreq 40%

basics

~20 s

First the superclass is constructed via super(...). Then, in source order, the class's field initializers and instance-initializer blocks run. Then the rest of the constructor body runs. So initializers come after super() but before your constructor code.

open as a page

What happens at compile time if a subclass constructor relies on an implicit super() but the parent class has no accessible no-argument constructor?

level: middleimportance: should knowfreq 45%

basics

~20 s

It won't compile. The compiler tries to insert a no-arg super() call, but if the parent has no accessible no-arg constructor, that call is invalid. You must add an explicit super(...) with the right arguments.

open as a page

When does the compiler's implicit super() insertion fail, and how do you fix it?

level: middleimportance: should knowfreq 58%

basics

~10 s

The compiler auto-adds super() only if the parent has a no-arg constructor. If the parent only has constructors that take arguments, you must call super(...) yourself with valid arguments, or the subclass won't compile.

open as a page

What is the copy constructor pattern in Java, and how do you implement a correct one?

level: middleimportance: should knowfreq 55%

basics

~20 s

A copy constructor takes another object of the same class and creates a new object with the same values. You write a constructor whose single parameter is the same type, then copy each field into the new object.

open as a page

What are the common reasons to make a constructor private in Java?

level: middleimportance: should knowfreq 60%

basics

~20 s

A private constructor stops other code from creating objects with new. You use it to force creation through factory methods, to make a singleton (only one instance), or for utility classes that hold only static members and should never be instantiated.

open as a page

What is constructor overloading in Java, and how does this() chaining work?

level: middleimportance: should knowfreq 70%

basics

~20 s

Constructor overloading means a class has several constructors with different parameter lists, so you can create objects in different ways. One constructor can call another in the same class using this(...) to avoid repeating setup code.

open as a page

Why is it dangerous to call an overridable (non-final, non-private) method from inside a constructor? What can go wrong?

level: seniorimportance: should knowfreq 55%

basics

~20 s

If a parent constructor calls a method the child overrides, the child's version runs while the child object isn't finished being built yet. The child's fields are still at their defaults (0/null), so the override can misbehave or crash.

open as a page

Walk through the exact order of execution when constructing an object in an inheritance hierarchy, including field initializers and chained this() calls.

level: seniorimportance: should knowfreq 52%

basics

~20 s

First the parent is constructed top-down (via super), then each class's instance field initializers and instance blocks run, then the rest of that constructor's body. A this() call defers to the target constructor before any field initializers in that class run.

open as a page

Can a constructor throw a checked exception in Java, and what are the implications?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Yes. A constructor can declare throws and throw checked exceptions just like a method. If it throws, the object is not created, so the caller must handle or declare the exception, and there is no half-built object returned.

open as a page

When you call new on a class with a parameterized constructor, what runs and in what order (including super, field initializers, and the constructor body)?

level: seniorimportance: should knowfreq 55%

basics

~20 s

First the parent class is built (super runs), then this class's field initializers and instance initializer blocks run top to bottom, and finally the rest of your constructor body runs. So the parent is always fully set up before your constructor code executes.

open as a page

You're designing a base class meant to be extended by others. What construction-order rules and design choices keep subclassing safe, and what should you do if you can't guarantee safety?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Don't call overridable methods from the base constructor; only call private/final/static ones. Document exactly what subclasses may do. Require needed state via super(...) parameters. If you can't make it safe, make the class final so it can't be subclassed.

open as a page

When does chaining many telescoping constructors become a design smell, and what alternatives would you reach for?

level: principalimportance: nice to knowfreq 40%

basics

~10 s

When a class has many parameters, especially optional ones, chained 'telescoping' constructors get hard to read and easy to misorder. Prefer a Builder, a static factory method, or a record/immutable object instead.

open as a page

Why is an enum often the best way to implement a singleton compared to a private-constructor class?

level: principalimportance: nice to knowfreq 35%

basics

~20 s

An enum with one constant is a singleton that Java guarantees stays single. Its constructor is private automatically, and Java prevents extra instances from reflection or deserialization — problems a hand-written private-constructor singleton must defend against itself.

open as a page

Constructors vs. static factory methods and the Builder pattern: when would you avoid exposing parameterized constructors directly?

level: principalimportance: nice to knowfreq 40%

basics

~20 s

Constructors are simple but limited: they must share the class name, can't return a cached or subclass object, and many similar parameters get confusing. Static factory methods and builders give named, flexible creation, so teams often prefer them for richer APIs.

open as a page