Site icon C1CTech

Kotlin Constructor in Inheritance

<p>In this article&comma; you will learn about Constructors &lpar;both primary and secondary constructor&rpar; behavior in Inheritance&comma; in Kotlin with the help of examples&period;<&sol;p>&NewLine;<p>A <span style&equals;"color&colon; &num;0000ff&semi;"><strong>constructor<&sol;strong><&sol;span> for a class is a special member function&comma; which is called implicitly&comma; just after the memory is allocated for the object to initialize the properties of the newly created object of that class type&period;<&sol;p>&NewLine;<p>Using Inheritance&comma; a <span style&equals;"color&colon; &num;0000ff&semi;"><strong>subclass<&sol;strong><&sol;span> can inherit all the members &lpar;properties and functions&rpar; from its <span style&equals;"color&colon; &num;0000ff&semi;"><strong>superclass<&sol;strong><&sol;span> &lpar;both those that are directly defined in the superclass and the ones that the superclass itself has inherited&rpar;&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Constructors<&sol;span> <&sol;strong>are not members&comma; 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>&period;<&sol;span><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Subclass Instance<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>When we create an object of a subclass&comma; both subclass and superclass members get memory in <strong><span style&equals;"color&colon; &num;008000&semi;">subclass<&sol;span><&sol;strong> 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 properties of a class&period; So both the constructor needs to be executed because this object &lpar;subclass&rpar; contains properties of both classes&period;<&sol;p>&NewLine;<p class&equals;"p1">In Kotlin&comma; the subclass must invoke one of the constructors &lpar;primary or secondary&rpar; of the base class&comma; passing either parameters from its own constructor or constant values and the super class constructor is always called before the subclass constructor&period;<&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Kotlin Inheritance Primary Constructor <&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">If the subclass contains a <span style&equals;"color&colon; &num;008000&semi;"><strong>primary<&sol;strong><&sol;span> constructor&comma; then we have to initialize the base class constructor &lpar;primary or secondary&rpar; using the parameters of the sub class&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Subclass primary constructor calling Superclass primary constructor&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;base class primary constructor with initializer block<&sol;span>&NewLine;open<&sol;strong><&sol;span> class Employee&lpar;name&colon; String&comma; age&colon; Int&rpar; &lbrace;&NewLine; init &lbrace;&NewLine; println&lpar;"My name is &dollar;name&comma; &dollar;age years old "&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;derived class inheriting base class<&sol;span><&sol;strong>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;derived class primary constructor with initializer block<&sol;strong><&sol;span>&NewLine;class Manager&lpar;name&colon; String&comma; age&colon; Int&comma; salary&colon; Int&rpar; &colon; Employee&lpar;name&comma; age&rpar; &lbrace;&NewLine; init &lbrace;&NewLine; println&lpar;"I am Manager and earning &dollar;salary per month&period;"&rpar;&NewLine; println&lpar;&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating derived class instance<&sol;span><&sol;strong>&NewLine; Manager&lpar;"Arun"&comma; 40&comma; 90000&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre>My name is Arun&comma; 40 years old &NewLine;I am Manager and earning 90000 per month&period;<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h5>&NewLine;<ul>&NewLine;<li>When we create an object of <strong><span style&equals;"color&colon; &num;008000&semi;">Manager<&sol;span><&sol;strong> &lpar;sub class&rpar;&comma; Manager class <strong><span style&equals;"color&colon; &num;008000&semi;">primary<&sol;span><&sol;strong> constructor will call&period;<&sol;li>&NewLine;<li>The Manager class local variables initialize with the respective values and pass the variable <strong><span style&equals;"color&colon; &num;0000ff&semi;">name<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>age<&sol;strong><&sol;span> as parameters to the <strong><span style&equals;"color&colon; &num;008000&semi;">Employee<&sol;span><&sol;strong> class&period;<&sol;li>&NewLine;<li>The Employee class <strong><span style&equals;"color&colon; &num;008000&semi;">init<&sol;span><&sol;strong> block will execute first and then the Manager class <strong><span style&equals;"color&colon; &num;008000&semi;">init<&sol;span><&sol;strong> block will execute&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Kotlin Inheritance Secondary Constructor <&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">If the subclass does not contain primary constructor&comma; then we have to call the superclass constructor &lpar;primary or secondary&rpar; from the secondary constructor of subclass using the <strong><span style&equals;"color&colon; &num;0000ff&semi;">super<&sol;span><&sol;strong> keyword&period; We also need to initialize the superclass secondary constructor using the parameters of subclass&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;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling parent primary constructor from child secondary constructor<&sol;strong><&sol;span><&sol;h4>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;base class primary constructor with initializer block<&sol;span><&sol;strong>&NewLine;open class Employee&lpar;name&colon; String&comma; age&colon; Int&rpar; &lbrace;&NewLine;&NewLine; init &lbrace;&NewLine; println&lpar;"My name is &dollar;name&comma; &dollar;age years old "&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;derived class inheriting base class<&sol;span><&sol;strong>&NewLine;class Manager &colon; Employee &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;secondary constructor calling base class primary constructor<&sol;span><&sol;strong>&NewLine; constructor&lpar;name&colon; String&comma; age&colon; Int&comma; salary&colon; Int&rpar; &colon; super&lpar;name&comma; age&rpar; &lbrace;&NewLine; println&lpar;"I am Manager and earning &dollar;salary per month&period;"&rpar;&NewLine; println&lpar;&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating derived class instance<&sol;span><&sol;strong>&NewLine; Manager&lpar;"Arun"&comma; 40&comma; 90000&rpar;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre>My name is Arun&comma; 40 years old &NewLine;I am Manager and earning 90000 per month&period;<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h5>&NewLine;<ul>&NewLine;<li>When we create an object of <strong><span style&equals;"color&colon; &num;008000&semi;">Manager<&sol;span><&sol;strong> &lpar;sub class&rpar;&comma; Manager class <span style&equals;"color&colon; &num;008000&semi;"><strong>secondary<&sol;strong><&sol;span> constructor will call&period;<&sol;li>&NewLine;<li>The Manager class &lpar;secondary constructor&rpar; local variables initialize with the respective values and pass the variable <strong><span style&equals;"color&colon; &num;0000ff&semi;">name<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>age<&sol;strong><&sol;span> as parameters to the <strong><span style&equals;"color&colon; &num;008000&semi;">Employee<&sol;span><&sol;strong> class&period;<&sol;li>&NewLine;<li>The Employee class <strong><span style&equals;"color&colon; &num;008000&semi;">init<&sol;span><&sol;strong> block will execute first and then the remaining statements of secondary constructor will execute&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling parent secondary constructor from child secondary constructor<&sol;strong><&sol;span><&sol;h4>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;base class<&sol;span><&sol;strong>&NewLine;open class Employee &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;base class secondary constructor<&sol;span><&sol;strong>&NewLine; constructor&lpar;name&colon; String&comma; age&colon; Int&rpar; &lbrace;&NewLine; println&lpar;"My name is &dollar;name&comma; &dollar;age years old "&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;derived class inheriting base class<&sol;span><&sol;strong>&NewLine;class Manager &colon; Employee &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;secondary constructor calling base class secondary constructor<&sol;span><&sol;strong>&NewLine; constructor&lpar;name&colon; String&comma; age&colon; Int&comma; salary&colon; Int&rpar; &colon; super&lpar;name&comma; age&rpar; &lbrace;&NewLine; println&lpar;"I am Manager and earning &dollar;salary per month&period;"&rpar;&NewLine; println&lpar;&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating derived class instance<&sol;span><&sol;strong>&NewLine; Manager&lpar;"Arun"&comma; 40&comma; 90000&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre>My name is Arun&comma; 40 years old &NewLine;I am Manager and earning 90000 per month&period;<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h5>&NewLine;<ul>&NewLine;<li>When we create an object of <strong><span style&equals;"color&colon; &num;008000&semi;">Manager<&sol;span><&sol;strong> &lpar;sub class&rpar;&comma; Manager class <strong><span style&equals;"color&colon; &num;008000&semi;">secondary<&sol;span><&sol;strong> constructor will call&period;<&sol;li>&NewLine;<li>The Manager class&lpar;secondary constructor&rpar; local variables initialize with the respective values and pass the variable <strong><span style&equals;"color&colon; &num;0000ff&semi;">name<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>age<&sol;strong><&sol;span> as parameters to the <strong><span style&equals;"color&colon; &num;008000&semi;">Employee<&sol;span><&sol;strong> class&period;<&sol;li>&NewLine;<li>The Employee class secondary constructor<strong><span style&equals;"color&colon; &num;000000&semi;"> <&sol;span><&sol;strong>will execute first and then the remaining statements of Manager secondary constructor will execute&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;&NewLine;

Exit mobile version