Site icon C1CTech

Java Constructor in Inheritance

<p>In this article&comma; you will learn about Constructor behavior in Inheritance&comma; in Java with the help of examples&period;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;000000&semi;">A<&sol;span><strong><span style&equals;"color&colon; &num;0000ff&semi;"> Constructor<&sol;span><&sol;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&period;<&sol;p>&NewLine;<p>Whenever a class &lpar;sub class&rpar; extends another class &lpar;super class&rpar;&comma; a subclass inherits all the members &lpar;fields&comma; methods&comma; and nested classes&rpar; from its super class&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;"><span style&equals;"color&colon; &num;008000&semi;"><strong>Constructors <&sol;strong><&sol;span>are not members&comma; <&sol;span><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">so they are not inherited by a subclass&comma; but <span style&equals;"color&colon; &num;0000ff&semi;"><strong>the constructor of the super class can be invoked from the sub class<&sol;strong><&sol;span>&period;<&sol;span><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Subclass Instance<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>When we create an object of a subclass&comma; both subclass and superclass members get memory in subclass object&period;<&sol;p>&NewLine;<p>The main motive of a <span style&equals;"color&colon; &num;008000&semi;"><strong>constructor<&sol;strong><&sol;span> is to initialize the member variables of a class&period; So both the constructor needs to be executed because this object &lpar;subclass&rpar; contains member variables of both classes&period; In Java&comma; the super class constructor is always called before the current constructor&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Parent Class Default Constructor<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>In Java&comma; the constructor of super class with no argument &lpar;default constructor&rpar; gets automatically called in subclass constructor&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class A &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;default constructor<&sol;span><&sol;strong>&NewLine; A&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"A Class Constructor Called "&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class B extends A &lbrace;&NewLine; B&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"B Class Constructor Called "&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;creating B class instance<&sol;span><&sol;strong>&NewLine; B obj &equals; new B&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>A Class Constructor Called &NewLine;B Class Constructor Called<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>When we create an object of B &lpar;Sub class&rpar;&comma; B class constructor will call and<br &sol;>&NewLine;before executing its code it will invoke default constructor of super class&period;<&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Super Keyword<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>To call superclass constructor in subclass explicitly we make use of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>super<&sol;strong> <&sol;span>keyword&period; But <span style&equals;"color&colon; &num;0000ff&semi;"><strong>super&lpar;&rpar;<&sol;strong> <&sol;span>must be the first statement in subclass constructor&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling parent class default constructor<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>When the super class has a default constructor&comma; it is not necessary to call it using the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>super<&sol;strong> <&sol;span>keyword&period; It is called automatically&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class A &lbrace;&NewLine; A&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"A Class Constructor Called "&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class B extends A &lbrace;&NewLine;&NewLine; B&lpar;&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;must be the first statement <&sol;strong><&sol;span>&NewLine; super&lpar;&rpar;&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;optional<&sol;strong><&sol;span>&NewLine; System&period;out&period;println&lpar;"B Class Constructor Called "&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; B obj &equals; new B&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>A Class Constructor Called &NewLine;B Class Constructor Called<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling parent class parameterized constructor<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>If we want to call parameterized constructor of superclass in subclass&comma; then we must call it using <strong><span style&equals;"color&colon; &num;008000&semi;">super&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class A &lbrace;&NewLine; int a&semi;&NewLine;&NewLine; A&lpar;int x&rpar; &lbrace;&NewLine; a &equals; x&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class B extends A &lbrace;&NewLine; int b&semi;&NewLine;&NewLine; B&lpar;int x&comma; int y&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;must be the first statement <&sol;strong><&sol;span>&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"> <strong>super&lpar;x&rpar;&semi;<&sol;strong><&sol;span>&NewLine; b &equals; y&semi;&NewLine; &rcub;&NewLine;&NewLine; void show&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"a &equals; " &plus; a &plus; "&comma; b &equals; " &plus; b&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; B obj &equals; new B&lpar;10&comma; 20&rpar;&semi;&NewLine; obj&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>a &equals; 10&comma; b &equals; 20<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>When we create an object of B &lpar;sub class&rpar;&comma; 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&equals;"color&colon; &num;008000&semi;">supe<&sol;span><span style&equals;"color&colon; &num;008000&semi;">r<&sol;span><span style&equals;"color&colon; &num;008000&semi;">&lpar;x&rpar;<&sol;span><&sol;strong> statement&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Default Constructor Example in Multilevel Inheritance<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class Main &lbrace;&NewLine;&NewLine; public static void main&lpar;String arg&lbrack;&rsqb;&rpar; &lbrace; &NewLine; A a &equals; new A&lpar;&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi;&NewLine; B b &equals; new B&lpar;&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi;&NewLine; C c &equals; new C&lpar;&rpar;&semi; &NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class A &lbrace;&NewLine; A&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Constructor A called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class B extends A &lbrace;&NewLine; B&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Constructor B called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class C extends B &lbrace;&NewLine; C&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Constructor C called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>Constructor A called&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&NewLine;Constructor A called&NewLine;Constructor B called&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&NewLine;Constructor A called&NewLine;Constructor B called&NewLine;Constructor C called<&sol;pre>&NewLine;<h5><&sol;h5>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Parameterized Constructor Example <&sol;strong><&sol;span><span style&equals;"color&colon; &num;000080&semi;"><strong>in Multilevel Inheritance<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class Main &lbrace;&NewLine; public static void main&lpar;String arg&lbrack;&rsqb;&rpar; &lbrace;&NewLine; A x &equals; new A&lpar;10&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi;&NewLine; B y &equals; new B&lpar;10&comma; 20&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi;&NewLine; C z &equals; new C&lpar;10&comma; 20&comma; 30&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class A &lbrace;&NewLine; int i&semi;&NewLine;&NewLine; A&lpar;int i&rpar; &lbrace;&NewLine; this&period;i &equals; i&semi;&NewLine; System&period;out&period;println&lpar;"Constructor A called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class B extends A &lbrace;&NewLine; int j&semi;&NewLine;&NewLine; B&lpar;int i&comma; int j&rpar; &lbrace;&NewLine; super&lpar;i&rpar;&semi;&NewLine; this&period;j &equals; j&semi;&NewLine; System&period;out&period;println&lpar;"Constructor B called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class C extends B &lbrace;&NewLine; int k&semi;&NewLine;&NewLine; C&lpar;int i&comma; int j&comma; int k&rpar; &lbrace;&NewLine; super&lpar;i&comma; j&rpar;&semi;&NewLine; this&period;k &equals; k&semi;&NewLine; System&period;out&period;println&lpar;"Constructor C called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>Constructor A called&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&NewLine;Constructor A called&NewLine;Constructor B called&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&NewLine;Constructor A called&NewLine;Constructor B called&NewLine;Constructor C called<&sol;pre>&NewLine;<p>If a super class contains two or more constructors&comma; but one of them is a default constructor which does not take any parameters&comma; then it is not necessary to call it using the <strong><span style&equals;"color&colon; &num;008000&semi;">super<&sol;span> <&sol;strong>keyword&period; It is called automatically&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>public class Main &lbrace;&NewLine; public static void main&lpar;String arg&lbrack;&rsqb;&rpar; &lbrace;&NewLine; A a &equals; new A&lpar;10&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi;&NewLine; B b &equals; new B&lpar;20&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi;&NewLine; C c &equals; new C&lpar;30&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class A &lbrace;&NewLine; int i&semi;&NewLine;&NewLine; A&lpar;int i&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;CONSTRUCTOR 1 - PARAMETERIZED CONSTRUCTOR<&sol;span><&sol;strong>&NewLine; &lbrace;&NewLine; this&period;i &equals; i&semi;&NewLine; System&period;out&period;println&lpar;"Class A parameterized Constructor is called"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; A&lpar;&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;CONSTRUCTOR 2 - DEFAULT CONSTRUCTOR<&sol;span><&sol;strong>&NewLine; &lbrace;&NewLine; i &equals; 100&semi;&NewLine; System&period;out&period;println&lpar;"Class A default Constructor is called"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;class B extends A &lbrace;&NewLine; int j&semi;&NewLine;&NewLine; B&lpar;int j&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;call CONSTRUCTOR 2&comma; which is a default constructor&period;<&sol;strong><&sol;span>&NewLine; this&period;j &equals; j&semi; &NewLine; System&period;out&period;println&lpar;"Class B Constructor is called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class C extends A &lbrace;&NewLine; C&lpar;int i&rpar; &lbrace; &NewLine; super&lpar;i&rpar;&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;call CONSTRUCTOR 1<&sol;strong><&sol;span>&NewLine; System&period;out&period;println&lpar;"Class C Constructor is called"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>Class A parameterized Constructor is called&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&NewLine;Class A default Constructor is called&NewLine;Class B Constructor is called&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&NewLine;Class A parameterized Constructor is called&NewLine;Class C Constructor is called<&sol;pre>&NewLine;<p>If a super class contains two or more constructors and there is no default constructor&comma; it is required that the sub class constructor specifically call the required super class constructor using <strong><span style&equals;"color&colon; &num;008000&semi;">super<&sol;span> <&sol;strong>keyword&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class A&NewLine;&lbrace;&NewLine; int i&semi;&NewLine; int j&semi;&NewLine;&NewLine; A&lpar;int i&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;CONSTRUCTOR 1<&sol;span><&sol;strong>&NewLine; &lbrace;&NewLine; this&period;i &equals; i&semi;&NewLine; j &equals; i&semi;&NewLine; &rcub;&NewLine;&NewLine; A&lpar;int i&comma; int j&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;CONSTRUCTOR 2<&sol;span><&sol;strong>&NewLine; &lbrace;&NewLine; this&period;i &equals; i&semi;&NewLine; this&period;j &equals; j&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;class B extends A&NewLine;&lbrace;&NewLine; int j&semi;&NewLine; B&lpar;int j&rpar;&NewLine; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;show compilation error that there is no default constructor<&sol;span><&sol;strong>&NewLine; this&period;j &equals; j&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class C extends A&NewLine;&lbrace;&NewLine; C&lpar;int i&rpar;&NewLine; &lbrace;&NewLine; super&lpar;i&rpar;&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; CONSTRUCTOR 1 called<&sol;strong><&sol;span>&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><&sol;h5>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Points to remember&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>Constructors<&sol;strong><&sol;span> are not inherited by the subclass but can be invoked from the sub class&period;<&sol;li>&NewLine;<li>The super class constructor is always called before the current constructor&period;<&sol;li>&NewLine;<li>Explicit call to super class constructor from sub class constructor can be made using <span style&equals;"color&colon; &num;008000&semi;"><strong>super <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">keyword<&sol;span><&sol;span>&period;<&sol;li>&NewLine;<li>Call to super class constructor using <span style&equals;"color&colon; &num;008000&semi;"><strong>super<&sol;strong> <&sol;span>keyword must be the first statement in the sub class constructor&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;008000&semi;">default<&sol;span><&sol;strong> constructor &lpar;with no argument&rpar; of super class  gets automatically called in subclass constructor&period; So there is no need to explicitly call default constructor of parent class using <span style&equals;"color&colon; &num;008000&semi;"><strong>super<&sol;strong> <&sol;span>keyword&period;<&sol;li>&NewLine;<li>If we want to call <strong><span style&equals;"color&colon; &num;008000&semi;">parameterized<&sol;span><&sol;strong> constructor of super class in sub class&comma; then we must call it explicitly using <strong><span style&equals;"color&colon; &num;008000&semi;">super <&sol;span><&sol;strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">keyword<&sol;span><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;&NewLine;

Exit mobile version