๐ Day 10 of My Automation Journey โ Tricky Constructor Questions (Inheritance in Java)
Source: Dev.to
๐ Constructors & Inheritance โ Tricky Scenarios Explained

Discussing tricky scenarios, letโs break this clearly and powerfully ๐ช
โ 1๏ธโฃ Golden Rule of Constructors in Inheritance
When a child object is created:
- Parent constructor executes first
- Then Child constructor executes
Because every childโclass constructor implicitly or explicitly calls super().
๐ฏ Scenario 1 โ Parent has a noโarg constructor
class Parent {
Parent() {
System.out.println("Parent No-Arg Constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child No-Arg Constructor");
}
public static void main(String[] args) {
new Child();
}
}
Output
Parent No-Arg Constructor
Child No-Arg Constructor
โ Java automatically inserts super();.
๐ฏ Scenario 2 โ Parent has only a parameterized constructor
class Parent {
Parent(int x) {
System.out.println("Parent Parameterized Constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child Constructor");
}
}
โ Compileโtime error!
Why? Java tries to insert super(); (the default noโarg call), but Parent does not have a noโarg constructor.
โ Correct way
class Child extends Parent {
Child() {
super(10); // must call explicitly
System.out.println("Child Constructor");
}
}
๐ฏ Scenario 3 โ Parent has both constructors
class Parent {
Parent() {
System.out.println("Parent No-Arg");
}
Parent(int x) {
System.out.println("Parent Parameterized");
}
}
class Child extends Parent {
Child() {
super(100); // choose which parent constructor to call
System.out.println("Child Constructor");
}
}
โ You can decide which parent constructor to invoke.
๐ฏ Scenario 4 โ Child has a parameterized constructor
class Child extends Parent {
Child(int y) {
super(y);
System.out.println("Child Parameterized");
}
}
โ Parent constructor runs first.
๐ฏ Scenario 5 โ Constructor chaining within the same class (this())
class Child extends Parent {
Child() {
this(10); // calls another constructor of the same class
System.out.println("Child No-Arg");
}
Child(int x) {
super(x);
System.out.println("Child Parameterized");
}
}
Important Rules
this()must be the first line in a constructor.super()must be the first line in a constructor.- You cannot use both
this()andsuper()in the same constructor.
๐ฏ Scenario 6 โ Multiโlevel inheritance
class GrandParent {
GrandParent() {
System.out.println("GrandParent");
}
}
class Parent extends GrandParent {
Parent() {
System.out.println("Parent");
}
}
class Child extends Parent {
Child() {
System.out.println("Child");
}
public static void main(String[] args) {
new Child();
}
}
Output
GrandParent
Parent
Child
โ Constructor calls happen top โ down.
๐ฏ Scenario 7 โ Parent constructor is private
class Parent {
private Parent() {
System.out.println("Private Constructor");
}
}
class Child extends Parent { }
โ Compileโtime error โ the child cannot access the private constructor.
๐ฏ Scenario 8 โ What if you donโt write any constructor?
- Java provides a default noโarg constructor only when no constructor is declared in the class.
- As soon as you write any constructor, the default one is not generated.
๐ฅ Very Important Tricky Question
class Parent {
Parent(int x) {
System.out.println("Parent");
}
}
class Child extends Parent {
Child() {
System.out.println("Child");
}
public static void main(String[] args) {
new Child();
}
}
โ What happens?
๐ Compileโtime error โ Parent lacks a noโarg constructor, and Child does not explicitly call super(int).
๐ง Memory CheatโSheet
| Situation | What Happens |
|---|---|
| Parent has no constructor | Java supplies a default noโarg constructor. |
| Parent has only a parameterized ctor | Child must call super(value). |
| Child constructor | Always calls a parent constructor (implicitly or explicitly). |
| Multiโlevel inheritance | Constructors execute from the topmost superclass downwards. |
super() | Calls the immediate parent constructor. |
this() | Calls another constructor in the same class. |
๐ About Wrapper Classes & โ100โฏ% OOPโ
- Primitive โ not an object.
- Wrapper โ an object that represents a primitive.
int a = 10; // primitive
Integer b = 10; // wrapper (object)
โ Java achieves โalmostโ 100โฏ% OOP by providing wrapper classes for all primitives.
๐ก About var (Javaโฏ10 Feature)
var x = 100; // type inferred as int
- Allowed only for local variables.
- Not allowed for:
- Class (field) variables
- Method parameters
- Return types
Date: 04/03/2026
Trainer: Nantha from
# Payilagam
๐ค **A Small Note**
I used ChatGPT to help me structure and refine this blog.