Constructors help create objects by setting up their starting values. Understanding how constructors run helps you know what happens first when you make an object.
Constructor execution flow in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Java
class ClassName { ClassName() { // constructor code here } }
A constructor has the same name as the class.
It runs automatically when you create an object with new.
Examples
Java
class Animal { Animal() { System.out.println("Animal constructor runs"); } }
Java
class Dog extends Animal { Dog() { super(); // calls Animal constructor System.out.println("Dog constructor runs"); } }
super() is not written, Java calls the parent constructor automatically first.Java
class Cat extends Animal { Cat() { System.out.println("Cat constructor runs"); } }
Sample Program
This program shows the order constructors run when creating a Dog object. The Animal constructor runs first, then the Dog constructor.
Java
class Animal { Animal() { System.out.println("Animal constructor starts"); } } class Dog extends Animal { Dog() { System.out.println("Dog constructor starts"); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); } }
Important Notes
Java always runs the parent class constructor before the child class constructor.
If you don't write super(), Java adds it automatically as the first line in the child constructor.
Constructors help set up objects step-by-step from parent to child.
Summary
Constructors run automatically when creating objects.
Parent class constructor runs before child class constructor.
Understanding this flow helps you control object setup clearly.
Practice
1. In Java, when you create an object of a child class, which constructor runs first?
easy
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]
Hint: Parent constructor always runs before child constructor [OK]
Common Mistakes:
- Thinking child constructor runs first
- Believing constructors run simultaneously
- Assuming constructors don't run automatically
2. Which of the following is the correct way to call a parent class constructor from a child class in Java?
easy
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]
Hint: Use super() to call parent constructor explicitly [OK]
Common Mistakes:
- Using this() instead of super()
- Trying parent() or base() which don't exist
- Omitting the call when needed
3. What is the output of the following Java code?
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();
}
}medium
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]
Hint: Parent constructor output appears before child output [OK]
Common Mistakes:
- Assuming child prints before parent
- Ignoring implicit super() call
- Expecting only one letter output
4. Consider this Java code snippet:
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?medium
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]
Hint: Default constructors chain automatically if no args [OK]
Common Mistakes:
- Expecting error without explicit constructors
- Thinking constructors don't chain automatically
- Mixing output order
5. Given these classes:
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?hard
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]
Hint: Parent constructors run before child, print in that order [OK]
Common Mistakes:
- Assuming SportsCar calls super() explicitly
- Mixing output order
- Forgetting implicit super() call in SportsCar
