In this article, you will learn about Constructor behavior in Inheritance, in Java with the help of examples.
A Constructor is a member function of a class that allows you to initialize a newly created object of that class type with some initial value and has same name as class with no explicit return type.
Whenever a class (sub class) extends another class (super class), a subclass inherits all the members (fields, methods, and nested classes) from its super class.
Constructors are not members, so they are not inherited by a subclass, but the constructor of the super class can be invoked from the sub class.
Creating Subclass Instance
When we create an object of a subclass, both subclass and superclass members get memory in subclass object.
The main motive of a constructor is to initialize the member variables of a class. So both the constructor needs to be executed because this object (subclass) contains member variables of both classes. In Java, the super class constructor is always called before the current constructor.
Parent Class Default Constructor
In Java, the constructor of super class with no argument (default constructor) gets automatically called in subclass constructor.
Example
class A { //default constructor A() { System.out.println("A Class Constructor Called "); } } class B extends A { B() { System.out.println("B Class Constructor Called "); } } class Main { public static void main(String[] args) { //creating B class instance B obj = new B(); } }
Output
A Class Constructor Called B Class Constructor Called
Example explained
When we create an object of B (Sub class), B class constructor will call and
before executing its code it will invoke default constructor of super class.
Super Keyword
To call superclass constructor in subclass explicitly we make use of super keyword. But super() must be the first statement in subclass constructor.
Calling parent class default constructor
When the super class has a default constructor, it is not necessary to call it using the super keyword. It is called automatically.
Example
class A { A() { System.out.println("A Class Constructor Called "); } } class B extends A { B() { //must be the first statement super(); //optional System.out.println("B Class Constructor Called "); } } class Main { public static void main(String[] args) { B obj = new B(); } }
Output
A Class Constructor Called B Class Constructor Called
Calling parent class parameterized constructor
If we want to call parameterized constructor of superclass in subclass, then we must call it using super().
Example
class A { int a; A(int x) { a = x; } } class B extends A { int b; B(int x, int y) { //must be the first statement super(x); b = y; } void show() { System.out.println("a = " + a + ", b = " + b); } } class Main { public static void main(String[] args) { B obj = new B(10, 20); obj.show(); } }
Output
a = 10, b = 20
Example explained
When we create an object of B (sub class), B class parameterized constructor will call and before executing its code it will execute the parameterized constructor of super class after reading super(x) statement.
Default Constructor Example in Multilevel Inheritance
public class Main { public static void main(String arg[]) { A a = new A(); System.out.println("********************"); B b = new B(); System.out.println("********************"); C c = new C(); } } class A { A() { System.out.println("Constructor A called"); } } class B extends A { B() { System.out.println("Constructor B called"); } } class C extends B { C() { System.out.println("Constructor C called"); } }
Output
Constructor A called ******************** Constructor A called Constructor B called ******************** Constructor A called Constructor B called Constructor C called
Parameterized Constructor Example in Multilevel Inheritance
public class Main { public static void main(String arg[]) { A x = new A(10); System.out.println("********************"); B y = new B(10, 20); System.out.println("********************"); C z = new C(10, 20, 30); } } class A { int i; A(int i) { this.i = i; System.out.println("Constructor A called"); } } class B extends A { int j; B(int i, int j) { super(i); this.j = j; System.out.println("Constructor B called"); } } class C extends B { int k; C(int i, int j, int k) { super(i, j); this.k = k; System.out.println("Constructor C called"); } }
Output
Constructor A called ******************** Constructor A called Constructor B called ******************** Constructor A called Constructor B called Constructor C called
If a super class contains two or more constructors, but one of them is a default constructor which does not take any parameters, then it is not necessary to call it using the super keyword. It is called automatically.
Example
public class Main { public static void main(String arg[]) { A a = new A(10); System.out.println("********************"); B b = new B(20); System.out.println("********************"); C c = new C(30); } } class A { int i; A(int i) //CONSTRUCTOR 1 - PARAMETERIZED CONSTRUCTOR { this.i = i; System.out.println("Class A parameterized Constructor is called"); } A() //CONSTRUCTOR 2 - DEFAULT CONSTRUCTOR { i = 100; System.out.println("Class A default Constructor is called"); } } class B extends A { int j; B(int j) { //call CONSTRUCTOR 2, which is a default constructor. this.j = j; System.out.println("Class B Constructor is called"); } } class C extends A { C(int i) { super(i); //call CONSTRUCTOR 1 System.out.println("Class C Constructor is called"); } }
Output
Class A parameterized Constructor is called ******************** Class A default Constructor is called Class B Constructor is called ******************** Class A parameterized Constructor is called Class C Constructor is called
If a super class contains two or more constructors and there is no default constructor, it is required that the sub class constructor specifically call the required super class constructor using super keyword.
Example
class A { int i; int j; A(int i) //CONSTRUCTOR 1 { this.i = i; j = i; } A(int i, int j) //CONSTRUCTOR 2 { this.i = i; this.j = j; } } class B extends A { int j; B(int j) { //show compilation error that there is no default constructor this.j = j; } } class C extends A { C(int i) { super(i); // CONSTRUCTOR 1 called } }
Points to remember:
- Constructors are not inherited by the subclass but can be invoked from the sub class.
- The super class constructor is always called before the current constructor.
- Explicit call to super class constructor from sub class constructor can be made using super keyword.
- Call to super class constructor using super keyword must be the first statement in the sub class constructor.
- The default constructor (with no argument) of super class gets automatically called in subclass constructor. So there is no need to explicitly call default constructor of parent class using super keyword.
- If we want to call parameterized constructor of super class in sub class, then we must call it explicitly using super keyword.