Constructor execution flow in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time taken by constructors grows as we create more objects or use inheritance.
How does the constructor's work change when the program runs?
Analyze the time complexity of the following code snippet.
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
super();
System.out.println("Child constructor");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
}
}
This code creates a Child object, which calls the Parent constructor first, then its own.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Constructor calls during object creation.
- How many times: Each constructor runs once per object creation; Parent constructor runs before Child constructor.
Each time we create a new Child object, both Parent and Child constructors run once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 constructor calls (10 Parent + 10 Child) |
| 100 | 200 constructor calls (100 Parent + 100 Child) |
| 1000 | 2000 constructor calls (1000 Parent + 1000 Child) |
Pattern observation: The total constructor calls grow linearly with the number of objects created.
Time Complexity: O(n)
This means the time to create n objects grows directly in proportion to n.
[X] Wrong: "The Parent constructor runs multiple times per object creation."
[OK] Correct: Each object creation calls the Parent constructor exactly once before the Child constructor.
Understanding constructor execution flow helps you explain object creation and inheritance clearly, a useful skill in many coding discussions.
"What if the Child constructor called another method that creates multiple objects? How would the time complexity change?"
Practice
Solution
Step 1: Understand constructor call order
In Java, when creating an object, the parent class constructor runs before the child class constructor.Step 2: Reason about object creation
This ensures the parent part of the object is set up before the child adds its own setup.Final Answer:
The parent class constructor -> Option AQuick Check:
Parent constructor runs first [OK]
- Thinking child constructor runs first
- Believing constructors run simultaneously
- Assuming constructors don't run automatically
Solution
Step 1: Recall Java syntax for parent constructor call
Java uses the keywordsuper()to call the parent class constructor explicitly.Step 2: Check other options
parent(),this(), andbase()are not valid Java syntax for this purpose.Final Answer:
super(); -> Option CQuick Check:
Use super() to call parent constructor [OK]
- Using this() instead of super()
- Trying parent() or base() which don't exist
- Omitting the call when needed
class Parent {
Parent() {
System.out.print("P");
}
}
class Child extends Parent {
Child() {
System.out.print("C");
}
}
public class Test {
public static void main(String[] args) {
new Child();
}
}Solution
Step 1: Identify constructor calls
Creatingnew Child()calls the Child constructor, which implicitly calls the Parent constructor first.Step 2: Trace output order
Parent constructor prints "P" first, then Child constructor prints "C".Final Answer:
PC -> Option BQuick Check:
Parent prints P, then Child prints C [OK]
- Assuming child prints before parent
- Ignoring implicit super() call
- Expecting only one letter output
class A {
A() {
System.out.print("A");
}
}
class B extends A {
B() {
System.out.print("B");
}
}
class C extends B {
C() {
System.out.print("C");
}
}
public class Main {
public static void main(String[] args) {
new C();
}
}
What is the output, and if there is an error, how to fix it?Solution
Step 1: Check constructor chaining
Each class has a default constructor that implicitly calls its parent constructor.Step 2: Trace constructor calls
Creatingnew C()calls C(), which calls B(), which calls A(), printing "A", then "B", then "C".Final Answer:
ABC -> Option AQuick Check:
Constructors chain up and print in order A-B-C [OK]
- Expecting error without explicit constructors
- Thinking constructors don't chain automatically
- Mixing output order
class Vehicle {
Vehicle() {
System.out.print("V");
}
}
class Car extends Vehicle {
Car() {
super();
System.out.print("C");
}
}
class SportsCar extends Car {
SportsCar() {
System.out.print("S");
}
}
public class Demo {
public static void main(String[] args) {
new SportsCar();
}
}
What is the output, and why does it appear in that order?Solution
Step 1: Analyze constructor calls
Creatingnew SportsCar()calls SportsCar(), which implicitly calls Car(), which calls Vehicle() viasuper().Step 2: Trace output sequence
Vehicle constructor prints "V", then Car prints "C", then SportsCar prints "S".Final Answer:
VCS -> Option DQuick Check:
Parent to child constructor output order is V-C-S [OK]
- Assuming SportsCar calls super() explicitly
- Mixing output order
- Forgetting implicit super() call in SportsCar
