๐Ÿš€ Day 10 of My Automation Journey โ€“ Tricky Constructor Questions (Inheritance in Java)

Published: (March 4, 2026 at 12:26 PM EST)
4 min read
Source: Dev.to

Source: Dev.to

๐Ÿ“š Constructors & Inheritance โ€“ Tricky Scenarios Explained

Constructor Flow Diagram

Discussing tricky scenarios, letโ€™s break this clearly and powerfully ๐Ÿ’ช


โœ… 1๏ธโƒฃ Golden Rule of Constructors in Inheritance

When a child object is created:

  1. Parent constructor executes first
  2. 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() and super() 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

SituationWhat Happens
Parent has no constructorJava supplies a default noโ€‘arg constructor.
Parent has only a parameterized ctorChild must call super(value).
Child constructorAlways calls a parent constructor (implicitly or explicitly).
Multiโ€‘level inheritanceConstructors 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.
0 views
Back to Blog

Related posts

Read more ยป

#7 Known is a drop! Inheritance in JAVA

Inheritance in Java Inheritance is a powerful objectโ€‘oriented programming feature offered by Java. It allows you to create a new class by reusing the functiona...

Part-4 Functional Interface(Function)

Functional Interface: Function A functional interface in java.util.function that represents a transformation: it takes an input of type T and produces an outpu...

Part-3 Functional Interface (Consumer)

Consumer Consumer is a functional interface in java.util.function. It represents an operation that takes one input and returns nothing. Core method java void a...