DEV Community

Cover image for πŸš€ Day 10 of My Automation Journey – Tricky Constructor Questions (Inheritance in Java)
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 10 of My Automation Journey – Tricky Constructor Questions (Inheritance in Java)

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Output:

Parent No-Arg Constructor
Child No-Arg Constructor
Enter fullscreen mode Exit fullscreen mode

βœ” 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

🎯 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ” 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ” Output

Parent Constructor
Child Parameterized Constructor
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Output

Parent Parameterized
Child Parameterized
Child No-Arg
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Output:

GrandParent
Parent
Child
Enter fullscreen mode Exit fullscreen mode

βœ” 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ” What happens?

Java automatically provides default constructors.

Equivalent to:

Parent() {}
Child() {}

Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

❓ 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
Enter fullscreen mode Exit fullscreen mode

πŸ’Ž About Wrapper Class & 100% OOP

Primitive β†’ Not Object
Wrapper β†’ Everything is Object

Example:

int a = 10;          // primitive
Integer b = 10;      // Wrapper (Object)
Enter fullscreen mode Exit fullscreen mode

βœ” Java achieves "almost" 100% OOP using wrapper classes.

πŸ’‘ About var (Java 10 Feature)

Introduced in Java 10

var x = 100;  // type inferred as int
Enter fullscreen mode Exit fullscreen mode

βœ” 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)