<p>In this article, you will learn about Constructor behavior in Inheritance, in Java with the help of examples.</p>
<p class="p1"><span style="color: #000000;">A</span><strong><span style="color: #0000ff;"> Constructor</span></strong> 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.</p>
<p>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.</p>
<p><span style="color: var(--color-text);"><span style="color: #008000;"><strong>Constructors </strong></span>are not members, </span><span style="color: var(--color-text);">so they are not inherited by a subclass, but <span style="color: #0000ff;"><strong>the constructor of the super class can be invoked from the sub class</strong></span>.</span></p>
<h4><span style="color: #000080;"><strong>Creating Subclass Instance</strong></span></h4>
<p>When we create an object of a subclass, both subclass and superclass members get memory in subclass object.</p>
<p>The main motive of a <span style="color: #008000;"><strong>constructor</strong></span> 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.</p>
<h4><span style="color: #000080;"><strong>Parent Class Default Constructor</strong></span></h4>
<p>In Java, the constructor of super class with no argument (default constructor) gets automatically called in subclass constructor.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class A {
 <strong><span style="color: #008000;"> //default constructor</span></strong>
 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) {
 <strong><span style="color: #008000;"> //creating B class instance</span></strong>
 B obj = new B();
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>A Class Constructor Called 
B Class Constructor Called</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<p>When we create an object of B (Sub class), B class constructor will call and<br />
before executing its code it will invoke default constructor of super class.</p>
<h3></h3>
<h4><strong><span style="color: #000080;">Super Keyword</span></strong></h4>
<p>To call superclass constructor in subclass explicitly we make use of <span style="color: #0000ff;"><strong>super</strong> </span>keyword. But <span style="color: #0000ff;"><strong>super()</strong> </span>must be the first statement in subclass constructor.</p>
<h5><span style="color: #000080;"><strong>Calling parent class default constructor</strong></span></h5>
<p>When the super class has a default constructor, it is not necessary to call it using the <span style="color: #0000ff;"><strong>super</strong> </span>keyword. It is called automatically.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class A {
 A() {
 System.out.println("A Class Constructor Called ");
 }
}

class B extends A {

 B() {
<span style="color: #008000;"><strong> //must be the first statement </strong></span>
 super(); <span style="color: #008000;"><strong>//optional</strong></span>
 System.out.println("B Class Constructor Called ");
 }
}

class Main {
 public static void main(String[] args) {
 B obj = new B();
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>A Class Constructor Called 
B Class Constructor Called</pre>
<h4></h4>
<h5><span style="color: #000080;"><strong>Calling parent class parameterized constructor</strong></span></h5>
<p>If we want to call parameterized constructor of superclass in subclass, then we must call it using <strong><span style="color: #008000;">super()</span></strong>.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class A {
 int a;

 A(int x) {
 a = x;
 }
}

class B extends A {
 int b;

 B(int x, int y) {
 <span style="color: #008000;"><strong>//must be the first statement </strong></span>
 <span style="color: #0000ff;"> <strong>super(x);</strong></span>
 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();

 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>a = 10, b = 20</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<p>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 <strong><span style="color: #008000;">supe</span><span style="color: #008000;">r</span><span style="color: #008000;">(x)</span></strong> statement.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p> ;</p>
<h4><span style="color: #000080;"><strong>Default Constructor Example in Multilevel Inheritance</strong></span></h4>
<pre>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");
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Constructor A called
********************
Constructor A called
Constructor B called
********************
Constructor A called
Constructor B called
Constructor C called</pre>
<h5></h5>
<h4><span style="color: #000080;"><strong>Parameterized Constructor Example </strong></span><span style="color: #000080;"><strong>in Multilevel Inheritance</strong></span></h4>
<pre>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");
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Constructor A called
********************
Constructor A called
Constructor B called
********************
Constructor A called
Constructor B called
Constructor C called</pre>
<p>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 <strong><span style="color: #008000;">super</span> </strong>keyword. It is called automatically.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>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) <strong><span style="color: #008000;">//CONSTRUCTOR 1 - PARAMETERIZED CONSTRUCTOR</span></strong>
 {
 this.i = i;
 System.out.println("Class A parameterized Constructor is called");
 }

 A() <strong><span style="color: #008000;">//CONSTRUCTOR 2 - DEFAULT CONSTRUCTOR</span></strong>
 {
 i = 100;
 System.out.println("Class A default Constructor is called");
 }

}

class B extends A {
 int j;

 B(int j) {
 <span style="color: #008000;"><strong>//call CONSTRUCTOR 2, which is a default constructor.</strong></span>
 this.j = j; 
 System.out.println("Class B Constructor is called");
 }
}

class C extends A {
 C(int i) { 
 super(i); <span style="color: #008000;"><strong>//call CONSTRUCTOR 1</strong></span>
 System.out.println("Class C Constructor is called");
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>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</pre>
<p>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 <strong><span style="color: #008000;">super</span> </strong>keyword.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class A
{
 int i;
 int j;

 A(int i) <strong><span style="color: #008000;">//CONSTRUCTOR 1</span></strong>
 {
 this.i = i;
 j = i;
 }

 A(int i, int j) <strong><span style="color: #008000;"> //CONSTRUCTOR 2</span></strong>
 {
 this.i = i;
 this.j = j;
 }

}

class B extends A
{
 int j;
 B(int j)
 {
 <strong><span style="color: #008000;">//show compilation error that there is no default constructor</span></strong>
 this.j = j;
 }
}

class C extends A
{
 C(int i)
 {
 super(i); <span style="color: #008000;"><strong>// CONSTRUCTOR 1 called</strong></span>
 }
}</pre>
<h5></h5>
<h4><span style="color: #000080;"><strong>Points to remember:</strong></span></h4>
<ul>
<li><span style="color: #008000;"><strong>Constructors</strong></span> are not inherited by the subclass but can be invoked from the sub class.</li>
<li>The super class constructor is always called before the current constructor.</li>
<li>Explicit call to super class constructor from sub class constructor can be made using <span style="color: #008000;"><strong>super </strong><span style="color: #000000;">keyword</span></span>.</li>
<li>Call to super class constructor using <span style="color: #008000;"><strong>super</strong> </span>keyword must be the first statement in the sub class constructor.</li>
<li>The <strong><span style="color: #008000;">default</span></strong> 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 <span style="color: #008000;"><strong>super</strong> </span>keyword.</li>
<li>If we want to call <strong><span style="color: #008000;">parameterized</span></strong> constructor of super class in sub class, then we must call it explicitly using <strong><span style="color: #008000;">super </span></strong><span style="color: #008000;"><span style="color: #000000;">keyword</span></span>.</li>
</ul>


