Inheritance
Constructor Chaining
Analyze subclass-to-parent execution flow, implicit super() injections, and critical constructor safety invariants.
Interview: Commonly tested: construction execution order, compiler default constructor rules, and dangers of invoking overridable methods in constructors.
Constructor Chaining is the sequence of constructor calls that occurs when an object is instantiated. When a subclass is constructed, the constructor of its superclass is invoked first, propagating top-down to java.lang.Object.
Top-Down Order
Constructors execute top-down: parent initializers and constructors run completely before child constructors execute.
Implicit super()
If you do not call super() or this(), the compiler automatically inserts an implicit, parameterless super() call.
Safety Invariant
Calling overridden methods inside parent constructors runs child code against uninitialized fields, causing bugs.
Constructor Chaining Execution Sequence
When you instantiate a subclass, the JVM follows a strict initialization order:
- Memory is allocated on the heap for the entire object, and fields are initialized to their default values (e.g., 0, false, null).
- The subclass constructor begins and delegates to the parent constructor using
super()(either explicitly or implicitly). - The parent class instance initializer blocks and field initializers execute.
- The parent constructor body executes.
- The subclass instance initializer blocks and field initializers execute.
- The subclass constructor body executes.
The Danger of Overridable Methods in Constructors
Calling an overridable method inside a parent constructor is a serious anti-pattern. If a subclass overrides that method, the overridden version will execute while the parent constructor is running. At this point, the subclass constructor has not yet run, and its field initializers have not executed. Any access to subclass instance variables inside the overridden method will read uninitialized default values, potentially causing NullPointerException or corrupted states.
Common Pitfalls
- Parent lacking a no-argument constructor: If a parent class declares a parameterized constructor and omits the default no-arg constructor, subclasses will fail to compile unless they explicitly call
super(args). - Invoking overridden methods in constructors: Invoking polymorphic methods inside superclass constructors, causing runtime failures due to uninitialized subclass fields.
Best Practices
- Keep Constructors Simple: Only perform field initialization inside constructors. Avoid complex business logic or method calls.
- Never Call Overridable Methods in Constructors: If you must call a method inside a constructor, make it
privateorfinalto prevent subclasses from overriding it.
Interview-Relevant Information
Q1: What happens if a superclass does not have a no-argument constructor?
Answer: The subclass constructor will fail to compile because it tries to insert an implicit super() call, which does not exist in the parent class. The developer must explicitly invoke a parameterized parent constructor using super(params) as the first statement.
Q2: Why do instance initializer blocks execute before the constructor body?
Answer: Initializer blocks are copied by the Java compiler into every constructor immediately after the super() call. This guarantees that all general instance initializations run before the specific constructor body executes.
Quick Checklist
Can you trace the execution order of parent constructors and subclass field initializers, explain when implicit super() is injected, and demonstrate why calling overridden methods inside constructors is dangerous? If yes, you understand constructor chaining.
Use Cases
Enforcing mandatory parameters during initialization of framework components (e.g., DB connections).
Setting up base configuration contexts in handler classes before specific subclasses execute.
Common Mistakes
Failing to declare an explicit super(...) call when the parent class has no default constructor.
Calling polymorphic methods inside super constructors and encountering NullPointerExceptions.