Java Programming Handbook

    Constructors in Inheritance

    In Java, when a class inherits another class, the constructor of the parent class is executed first, followed by the constructor of the child class. This ensures that the base class is properly initialized before the derived class adds its own functionality.

    How Constructors Work in Inheritance#

    When an object of a derived class is created, the constructor of the base class is automatically called first. If there is no explicit call to the superclass constructor, Java automatically invokes the default constructor of the superclass.

    Example 1: Default Constructor in Inheritance#

    class Parent { Parent() { System.out.println("Parent class constructor called"); } } class Child extends Parent { Child() { System.out.println("Child class constructor called"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); } }

    Output:

    Parent class constructor called Child class constructor called

    Explanation:#

    • When we create an object of Child, the constructor of Parent is called first.
    • After that, the constructor of Child is executed.

    Using super() to Call Parameterized Constructor#

    By default, the Java compiler automatically calls the parent class’s default constructor. However, if the parent class does not have a default constructor, we must explicitly call a parameterized constructor using super().

    Example 2: Using super() to Call Parameterized Constructor#

    class Parent { Parent(String name) { System.out.println("Parent constructor called. Name: " + name); } } class Child extends Parent { Child(String name) { super(name); // Calling the parent constructor System.out.println("Child constructor called."); } } public class Main { public static void main(String[] args) { Child obj = new Child("John"); } }

    Output:

    Parent constructor called. Name: John Child constructor called.

    Explanation:#

    • The super(name) call explicitly invokes the constructor of the parent class.
    • This ensures that the parent class is initialized properly before executing the child class constructor.

    Constructor Chaining in Inheritance#

    Constructor chaining refers to the process where one constructor calls another constructor in the same or parent class using this() or super().

    Example 3: Constructor Chaining#

    class A { A() { System.out.println("Constructor of A"); } } class B extends A { B() { super(); // Calls the constructor of A System.out.println("Constructor of B"); } } class C extends B { C() { super(); // Calls the constructor of B System.out.println("Constructor of C"); } } public class Main { public static void main(String[] args) { C obj = new C(); } }

    Output:

    Constructor of A Constructor of B Constructor of C

    Explanation:#

    • The super() calls ensure that constructors are called from the base class to the derived class in the correct order.

    Conclusion#

    In this blog, we learned how constructors behave in Java inheritance. We explored how superclass constructors are called before subclass constructors, how to use super() to call parameterized constructors, and the concept of constructor chaining. Understanding these concepts helps in building efficient object-oriented Java applications.

    Last updated on Apr 09, 2025