Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Constructor Execution Flow in Java
π Scenario: Imagine you are creating a simple Java program to understand how constructors work when you create objects. Constructors are special methods that run automatically when you make a new object. This helps set up the object with the right starting values.
π― Goal: You will build a Java class with two constructors: one default and one with a parameter. You will see how Java runs these constructors step-by-step when you create objects.
π What You'll Learn
Create a class called Car with two constructors
One constructor should have no parameters and print a message
The other constructor should take a String parameter and print a message including the parameter
Create two Car objects: one using the default constructor and one using the parameterized constructor
Print the messages to show the order in which constructors run
π‘ Why This Matters
π Real World
Constructors are used in real-world Java programs to set up objects with initial values automatically when they are created.
πΌ Career
Understanding constructor execution flow is essential for Java developers to write clean, efficient, and bug-free code when working with classes and objects.
Progress0 / 4 steps
1
Create the Car class with a default constructor
Create a public class called Car. Inside it, write a public constructor with no parameters that prints "Default constructor called".
Java
Hint
Remember, a constructor has the same name as the class and no return type.
2
Add a parameterized constructor to Car
Add a second public constructor to the Car class that takes a String parameter called model. Inside it, print "Parameterized constructor called for model: " followed by the model value.
Java
Hint
Use + model to add the model name to the printed message.
3
Create two Car objects in Main class
Create a public class called Main with a main method. Inside main, create one Car object using the default constructor and another Car object using the parameterized constructor with the model name "Tesla".
Java
Hint
Use new Car() for the default constructor and new Car("Tesla") for the parameterized one.
4
Run the program and print constructor execution messages
Run the Main class. The program should print the messages from both constructors showing the order they run.
Java
Hint
When you run the program, the messages from both constructors should appear in the console.
Practice
(1/5)
1. In Java, when you create an object of a child class, which constructor runs first?
easy
A. The parent class constructor
B. The child class constructor
C. Both constructors run simultaneously
D. No constructor runs automatically
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 A
Quick 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
A. this();
B. parent();
C. super();
D. base();
Solution
Step 1: Recall Java syntax for parent constructor call
Java uses the keyword super() to call the parent class constructor explicitly.
Step 2: Check other options
parent(), this(), and base() are not valid Java syntax for this purpose.
Final Answer:
super(); -> Option C
Quick 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
A. CP
B. PC
C. C
D. P
Solution
Step 1: Identify constructor calls
Creating new 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 B
Quick 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
A. ABC
B. Error: No default constructor in A
C. Error: Constructor call missing in B
D. BAC
Solution
Step 1: Check constructor chaining
Each class has a default constructor that implicitly calls its parent constructor.
Step 2: Trace constructor calls
Creating new C() calls C(), which calls B(), which calls A(), printing "A", then "B", then "C".
Final Answer:
ABC -> Option A
Quick 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
A. CSV
B. VSC
C. SVC
D. VCS
Solution
Step 1: Analyze constructor calls
Creating new SportsCar() calls SportsCar(), which implicitly calls Car(), which calls Vehicle() via super().
Step 2: Trace output sequence
Vehicle constructor prints "V", then Car prints "C", then SportsCar prints "S".
Final Answer:
VCS -> Option D
Quick Check:
Parent to child constructor output order is V-C-S [OK]
Hint: Parent constructors run before child, print in that order [OK]