In this tutorial, we will look at order of constructor call in case of inherited classes.

When an object of Derived class is created, default base class constructor is called.

Consider the following example.

class Base

{

        Base()

        {

               System.out.println(“Base Class Constructor”);

        }

}

 public class Inherit extends Base{

         Inherit()

        {

               System.out.println(“Derived Class Constructor”);

        }

        public static void main(String[] args) {

                Inherit i = new Inherit();

        }

}

OUTPUT

Base Class Constructor

Derived Class Constructor

 

Compiler would insert the super() statement implicitly in the derived class constructor.

For a given derived class, super() function would call the base class constructor automatically.

Lets add the super() call in the derived class constructor.

class Base

{

        Base()

        {

               System.out.println(“Base Class Constructor”);

        }

}

 

public class Inherit extends Base{

        Inherit()

        {

               super(); //Make a note of this statement..

               System.out.println(“Derived Class Constructor”);

        }

        public static void main(String[] args) {

                Inherit i = new Inherit();

        }

}

 

OUTPUT

Base Class Constructor

Derived Class Constructor

 

Please note the constructor adds only the super() function, there is no super(int) or any such statements added by the compiler.

The following code explains the same.

class Base

{

        Base()

        {

               System.out.println(“Base Class Constructor”);

        }

           Base(int a)

        {

               System.out.println(“Base Class Parameterized Constructor”);

        }

}

public class Inherit extends Base{

         Inherit(int a)

        {

                System.out.println(“Derived Class Parameterized Constructor”);

        }

        public static void main(String[] args) {

                Inherit i = new Inherit(10);

        }

}

OUTPUT

Base Class Constructor

Derived Class Parameterized Constructor

 

Even though the base class has a parameterized constructor, the constructor that’s called is the one with no arguments.

So, what do you do if you want to call the base class parameterized constructor??

Add, super(int a) explicitly.

class Base

{

        Base()

        {

               System.out.println(“Base Class Constructor”);

        }

        Base(int a)

        {

               System.out.println(“Base Class Parameterized Constructor”);

        }

 }

 public class Inherit extends Base{

           Inherit(int a)

        {

               super(a);

               System.out.println(“Derived Class Parameterized Constructor”);

        }

        public static void main(String[] args) {

               Inherit i = new Inherit(10);

        }

}

OUTPUT

Base Class Parameterized Constructor

Derived Class Parameterized Constructor