Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Superclass constructor invocation is the mechanism by which a subclass constructor explicitly or implicitly calls the constructor of its immediate parent class during object instantiation. This process ensures that the inherited state of an object is properly initialized before the subclass-specific initialization occurs, establishing a strict top-down initialization sequence known as constructor chaining. In Java, this invocation is handled using the super keyword followed by parentheses containing any required arguments.
class SuperClass {
    public SuperClass(int arg1) {
        // Superclass initialization logic
    }
}

public class SubClass extends SuperClass {
    public SubClass(int arg1, String arg2) {
        super(arg1); // Explicit superclass constructor invocation
    }
}

Core Mechanics and Rules

1. Placement and Flexible Constructor Bodies Historically (Java 21 and earlier), an explicit superclass constructor invocation was required to be the absolute first statement within the subclass constructor’s body. Starting with Java 22 (JEP 447) and Java 23 (JEP 482) via “Flexible Constructor Bodies,” Java allows pre-construction statements before the super() call. These statements can include local variable declarations, argument validation, or any logic that does not reference the instance being created. 2. Pre-Construction Context Arguments passed to super(...) are evaluated in a strict pre-construction context. Because the superclass has not yet been initialized, these arguments cannot reference the current instance (this), instance variables, or instance methods of the subclass being instantiated. However, Java allows any expression that does not depend on the current instance. This includes parameters passed into the subclass constructor, static variables, static methods, literals (e.g., super(10) or super("text")), object allocations (e.g., super(new ArrayList<>())), and method calls on independent objects. 3. Implicit Invocation If a subclass constructor does not explicitly invoke a superclass constructor via super(...), and does not invoke an overloaded constructor in the same class via this(...), the Java compiler automatically inserts a no-argument super(); call.
class Parent {
    public Parent() {
    }
}

// Developer-written code:
class ImplicitChild extends Parent {
    public ImplicitChild() {
        System.out.println("Child initialized");
    }
}

// Compiler-generated equivalent:
class ExplicitEquivalentChild extends Parent {
    public ExplicitEquivalentChild() {
        super(); // Implicitly inserted by the compiler
        System.out.println("Child initialized");
    }
}
4. Compilation Dependencies Because of the implicit invocation rule, if a superclass defines only parameterized constructors (meaning it lacks a no-argument constructor), the subclass must explicitly invoke one of those parameterized constructors using super(args). Failure to do so causes a compilation error because the compiler’s implicitly inserted super(); will fail to resolve to an existing constructor in the parent class. 5. Mutual Exclusivity with this() A constructor can contain either an explicit superclass constructor invocation (super()) or an explicit alternate constructor invocation (this()), but never both. If this() is used, the superclass constructor invocation is deferred to the target constructor being called within the same class.

Qualified Superclass Constructor Invocation

When a class extends a non-static inner class, the inner class relies on a hidden reference to its enclosing instance. To instantiate the subclass, the Java compiler requires an explicit reference to the enclosing class. This is handled via a Qualified Superclass Constructor Invocation using the syntax enclosingInstance.super(...).
class Outer {
    class Inner {
        Inner() {}
    }
}

class SubInner extends Outer.Inner {
    // The subclass constructor must be provided an instance of the enclosing class
    SubInner(Outer enclosingInstance) {
        enclosingInstance.super(); // Qualified superclass constructor invocation
    }
}

Constructor Chaining Execution Order

When a subclass is instantiated, the constructor invocation propagates up the inheritance hierarchy until it reaches the root java.lang.Object class. The actual execution of initialization logic happens in a strict top-down order. For every class in the hierarchy, starting from Object and moving down to the instantiated subclass, the following sequence occurs:
  1. Superclass Constructor Execution: The superclass constructor body executes (propagating from Object downwards).
  2. Instance Initialization: Immediately after the superclass constructor returns, the current class’s instance variable initializers and instance initialization blocks execute in their textual order.
  3. Subclass Constructor Execution: Finally, the remainder of the current subclass constructor body executes.
This guarantees that a parent class is fully initialized—including its variables and initialization blocks—before the child class’s initialization blocks or constructor body attempt to access or modify the inherited state.
Master Java with Deep Grasping Methodology!Learn More