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 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 Parameterized Constructor
class Parent {
Parent(int x) {
System.out.println("Parent Parameterized Constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child Constructor");
}
public static void main(String[] args) {
new Child();
}
}
β Compile Time Error!
Why?
Because Java tries to insert:
super(); // default
But Parent does NOT have no-arg constructor.
β
Correct Way
class Parent {
Parent(int x) {
System.out.println("Parent Parameterized Constructor");
}
}
class Child extends Parent {
Child() {
super(10);
System.out.println("Child Constructor");
}
public static void main(String[] args) {
new Child();
}
}
π― Scenario 3: Both Constructors Present in Parent
class Parent {
Parent() {
System.out.println("Parent No-Arg Constructor");
}
Parent(int x) {
System.out.println("Parent Parameterized Constructor");
}
}
class Child extends Parent {
Child() {
super(10); // choosing which constructor to call
System.out.println("Child Constructor");
}
public static void main(String[] args) {
new Child();
}
}
β You can choose which parent constructor to call.
π― Scenario 4: Child Has Parameterized Constructor
class Parent {
Parent() {
System.out.println("Parent Constructor");
}
}
class Child extends Parent {
Child(int x) {
System.out.println("Child Parameterized Constructor");
}
public static void main(String[] args) {
new Child(10);
}
}
β Output
Parent Constructor
Child Parameterized Constructor
Parent constructor runs first.
π― Scenario 5: Constructor Chaining in Same Class (this())
class Parent {
Parent(int x) {
System.out.println("Parent Parameterized");
}
}
class Child extends Parent {
Child() {
this(10);
System.out.println("Child No-Arg");
}
Child(int x) {
super(x);
System.out.println("Child Parameterized");
}
public static void main(String[] args) {
new Child();
}
}
πΉ Output
Parent Parameterized
Child Parameterized
Child No-Arg
π₯ Important Rule:
this() must be first line
super() must be first line
You cannot use both in 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 call happens TOP β DOWN
π― Scenario 7: If Parent Constructor is PRIVATE
class Parent {
private Parent() {
System.out.println("Private Constructor");
}
}
class Child extends Parent {
public static void main(String[] args) {
new Child();
}
}
β Compile Error
Because child cannot access private constructor.
π― Scenario 8: What Happens If You Don't Write Any Constructor?
class Parent {
}
class Child extends Parent {
public static void main(String[] args) {
new Child();
}
}
β What happens?
Java automatically provides default constructors.
Equivalent to:
Parent() {}
Child() {}
Java provides default constructor automatically.
But ONLY if you donβt write any constructor.
If you write even ONE constructor β default is NOT created.
π₯ 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
Because Parent doesnβt have no-arg constructor.
π§ Memory Trick
Situation What Happens
Parent has no constructor Java gives default
Parent has only parameterized Child must call super(value)
Child constructor Always calls Parent
Multi-level Top to bottom execution
super() Calls parent constructor
this() Calls same class constructor
π About Wrapper Class & 100% OOP
Primitive β Not Object
Wrapper β Everything is Object
Example:
int a = 10; // primitive
Integer b = 10; // Wrapper (Object)
β Java achieves "almost" 100% OOP using wrapper classes.
π‘ About var (Java 10 Feature)
Introduced in Java 10
var x = 100; // type inferred as int
β Allowed only for local variables
β Not allowed for:
Class 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.

Top comments (0)