Site icon C1CTech

Kotlin Constructor

<p>In this article&comma; you will learn about constructors in Kotlin &lpar;both primary and secondary constructors&rpar; as well as initializer blocks with the help of examples&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Constructor<&sol;strong><&sol;span><&sol;h3>&NewLine;<ul>&NewLine;<li>A constructor for a class is a special member function&comma; mainly used to initialize the properties of the newly created object of that class type&period;<&sol;li>&NewLine;<li>It is called implicitly&comma; just after the memory is allocated for the object&period;<&sol;li>&NewLine;<li>It is not mandatory for the programmer to write a constructor for a class&period;<&sol;li>&NewLine;<li>When there is no constructor defined in the class by the programmer&comma; the compiler implicitly provides a default constructor for the class&period;<&sol;li>&NewLine;<li>In Kotlin&comma; the default visibility of the constructor is <strong><span style&equals;"color&colon; &num;008000&semi;">public<&sol;span><&sol;strong>&period; However&comma; the visibility can be changed to <span style&equals;"color&colon; &num;008000&semi;"><strong>private&comma;<&sol;strong> <strong>protected or internal<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Types of Constructor<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>In Kotlin&comma; there are two types of constructors&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Primary constructor<&sol;span><&sol;strong> &&num;8211&semi;  initializes the class&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Secondary constructor<&sol;strong><&sol;span> &&num;8211&semi; initializes the class and allows you to put additional logic&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Primary Constructor<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">primary<&sol;span> <span style&equals;"color&colon; &num;008000&semi;">constructor<&sol;span><&sol;strong> is part of the class header &lpar;contains the type parameters&comma; the primary constructor&comma; etc&period;&rpar;&comma; goes after the class name&comma; using the <strong><span style&equals;"color&colon; &num;0000ff&semi;">constructor<&sol;span><&sol;strong> keyword&period;<&sol;p>&NewLine;<p>In Kotlin&comma; a class can have at most one primary constructor and the parameters are optional&period;<&sol;p>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Addition is the class&lowbar;name<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;constructor&lpar;val a&colon; Int&comma; val b&colon; Int&rpar; is the class&lowbar;header<&sol;span><&sol;strong>&NewLine;class Addition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>constructor<&sol;strong><&sol;span>&lpar;var a&colon; Int&comma; var b&colon; Int&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;body of class<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">constructor<&sol;span><&sol;strong> keyword can be omitted if there is no annotation or visibility modifier specified&period;<&sol;p>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;constructor keyword optional&NewLine;&sol;&sol;without annotation or access modifier<&sol;span><&sol;strong>&NewLine;class Addition&lpar;val a&colon; Int&comma; val b&colon; Int&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; body of class<&sol;span><&sol;strong>&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;constructor keyword is needed&NewLine;<&sol;span><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;with<&sol;span><&sol;strong> <strong><span style&equals;"color&colon; &num;008000&semi;">annotation or access modifier<&sol;span><&sol;strong>&NewLine;class Addition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>private &commat;Inject constructor<&sol;strong><&sol;span>&lpar;val a&colon; Int&comma; val b&colon; Int&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; body of class<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&colon; Primary constructor<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;main function<&sol;span><&sol;strong>&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; var person &equals; Person&lpar;"Arun"&comma; 28&rpar;&NewLine; println&lpar;"FirstName&colon; &dollar;&lbrace;person&period;pers&lowbar;fname&rcub; &bsol;n Age&colon; &dollar;&lbrace;person&period;pers&lowbar;age&rcub;"&rpar;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;primary constructor&NewLine;&sol;&sol;constructor keyword is optional&NewLine;<&sol;span><&sol;strong>class Person constructor&lpar;<span style&equals;"color&colon; &num;008000&semi;"><strong>val pers&lowbar;fname&colon; String&comma; var pers&lowbar;age&colon; Int<&sol;strong><&sol;span>&rpar; &lbrace;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>FirstName&colon; Arun&NewLine;Age&colon; 28<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>When the object of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Person<&sol;strong><&sol;span> class is created&comma;  values<span style&equals;"color&colon; &num;008000&semi;"> <strong>&&num;8220&semi;Arun&&num;8221&semi;<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>28<&sol;strong><&sol;span> gets passed to the constructor&period;<&sol;li>&NewLine;<li>The constructor parameters <span style&equals;"color&colon; &num;0000ff&semi;"><strong>pers&lowbar;fname<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>pers&lowbar;<&sol;strong><strong>age<&sol;strong><&sol;span> get initialized with the values <strong>Arun<&sol;strong> and <strong>28<&sol;strong> respectively&period;<&sol;li>&NewLine;<li>Inside <strong><span style&equals;"color&colon; &num;008000&semi;">main&lpar;&rpar;<&sol;span><&sol;strong>&comma; we can directly access the constructor parameters just like we access class properties&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Primary Constructor with Initializer Block<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>The primary constructor cannot contain any code&comma; the initialization code can be placed in a separate initializer block prefixed with the <strong><span style&equals;"color&colon; &num;008000&semi;">init<&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><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;main function<&sol;span><&sol;strong>&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; var person &equals; Person&lpar;"Arun"&comma; 28&rpar;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;primary constructor<&sol;span><&sol;strong>&NewLine;class Person <strong><span style&equals;"color&colon; &num;0000ff&semi;">constructor<&sol;span><&sol;strong>&lpar;pers&lowbar;fname&colon; String&comma; pers&lowbar;age&colon; Int&rpar; &lbrace;&NewLine;&NewLine; val firstName&colon; String&NewLine; var age&colon; Int&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; initializer block<&sol;span><&sol;strong>&NewLine; init &lbrace;&NewLine; firstName &equals; pers&lowbar;fname&NewLine; age &equals; pers&lowbar;age&NewLine;&NewLine; println&lpar;"FirstName&colon; &dollar;firstName"&rpar;&NewLine; println&lpar;"Age&colon; &dollar;age"&rpar;&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>FirstName&colon; Arun&NewLine;Age&colon; 28<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>When the object <span style&equals;"color&colon; &num;008000&semi;"><strong>person<&sol;strong><&sol;span> is created for the class <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Person<&sol;strong><&sol;span>&comma; the values <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;Arun&&num;8221&semi;<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;008000&semi;"><strong>28<&sol;strong><&sol;span> gets passed to the parameter <span style&equals;"color&colon; &num;0000ff&semi;"><strong>pers&lowbar;fname<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>pers&lowbar;age<&sol;strong><&sol;span> of the constructor&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>firstName<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>age <&sol;strong><&sol;span>are the two properties of <strong>Person<&sol;strong> class&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>Initializer block<&sol;strong><&sol;span> is executed at the time of object creation&comma; and not only initialize the properties but also prints to the standard output&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Default values in <&sol;span><&sol;strong><strong><span style&equals;"color&colon; &num;000080&semi;">Primary Constructor <&sol;span><&sol;strong><&sol;h4>&NewLine;<p>You need not pass all the parameters while declaring an object&period; You can initialize the constructor parameters with some default values&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;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example <&sol;strong><&sol;span><&sol;h5>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;main function<&sol;span><&sol;strong>&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; var person1 &equals; Person&lpar;"Ronald"&comma; 28&rpar;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;default value for age will be used here<&sol;span><&sol;strong>&NewLine; var person2 &equals; Person&lpar;"Rohit"&rpar;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;default values for both parameters is used because no arguments passed<&sol;span> <&sol;strong>&NewLine; var person3 &equals; Person&lpar;&rpar;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;primary constructor<&sol;span><&sol;strong>&NewLine;class Person constructor&lpar;pers&lowbar;fname&colon; String &equals; "Arun"&comma; pers&lowbar;age&colon; Int &equals; 26&rpar; &lbrace;&NewLine;&NewLine; val firstName&colon; String&NewLine; var age&colon; Int&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; initializer block<&sol;span><&sol;strong>&NewLine; init &lbrace;&NewLine; firstName &equals; pers&lowbar;fname&NewLine; age &equals; pers&lowbar;age&NewLine;&NewLine; println&lpar;"FirstName&colon; &dollar;firstName &comma; Age&colon; &dollar;age"&rpar;&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>FirstName&colon; Ronald &comma; Age&colon; 28&NewLine;FirstName&colon; Rohit &comma; Age&colon; 26&NewLine;FirstName&colon; Arun &comma; Age&colon; 26<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>Here&comma; we have initialized the constructor parameters with some default values <span style&equals;"color&colon; &num;008000&semi;"><strong>pers&lowbar;<&sol;strong><strong><span style&equals;"color&colon; &num;008000&semi;">fname &equals; <&sol;span>&&num;8220&semi;Arun&&num;8221&semi; and pers&lowbar;age &equals; 26<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>So&comma; whenever we skip the parameter values while creating objects&comma; the compiler will automatically initialize the object properties with the default values&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Secondary Constructor<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">Secondary constructor<&sol;span><&sol;strong> initializes the class &lpar;properties&rpar; and allows you to put additional logic to the class as well&period;<&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;"> They are prefixed with the <&sol;span><span style&equals;"color&colon; &num;008000&semi;"><strong>constructor<&sol;strong><&sol;span><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;"> keyword and<&sol;span> are written inside the body of class&period;<&sol;p>&NewLine;<p>In Kotlin&comma; a class may have one or more secondary constructors and which secondary constructor will be called is decided by the compiler based on the arguments received&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&colon; Secondary constructor<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; Addition&lpar;2&comma; 3&rpar;&NewLine; Addition&lpar;2&comma; 3&comma; 4&rpar;&NewLine; Addition&lpar;2&comma; 3&comma; 4&comma; 5&rpar;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;class with three secondary constructors<&sol;span><&sol;strong>&NewLine;class Addition &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;secondary constructor with two arguments<&sol;span><&sol;strong>&NewLine; constructor&lpar;a&colon; Int&comma; b&colon; Int&rpar; &lbrace;&NewLine; var c &equals; a &plus; b&NewLine; println&lpar;"Sum of 2&comma; 3 &colon; &dollar;&lbrace;c&rcub;"&rpar;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;secondary constructor with three arguments<&sol;span><&sol;strong>&NewLine; constructor&lpar;a&colon; Int&comma; b&colon; Int&comma; c&colon; Int&rpar; &lbrace;&NewLine; var d &equals; a &plus; b &plus; c&NewLine; println&lpar;"Sum of 2&comma; 3&comma; 4 &colon; &dollar;&lbrace;d&rcub;"&rpar;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;secondary constructor with four arguments<&sol;span><&sol;strong>&NewLine; constructor&lpar;a&colon; Int&comma; b&colon; Int&comma; c&colon; Int&comma; d&colon; Int&rpar; &lbrace;&NewLine; var e &equals; a &plus; b &plus; c &plus; d&NewLine; println&lpar;"Sum of 2&comma; 3&comma; 4&comma; 5 &colon; &dollar;&lbrace;e&rcub;"&rpar;&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>Sum of 2&comma; 3 &colon; 5&NewLine;Sum of 2&comma; 3&comma; 4 &colon; 9&NewLine;Sum of 2&comma; 3&comma; 4&comma; 5 &colon; 14<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>Firstly&comma; the secondary constructor &lpar;with two arguments&rpar; is called&comma; which prints the sum of <strong>two<&sol;strong> numbers&period;<&sol;li>&NewLine;<li>Then&comma;  the secondary constructor &lpar;with three arguments&rpar; is called&comma; which prints the sum of <strong>three<&sol;strong> numbers&period;<&sol;li>&NewLine;<li>And then&comma; the secondary constructor &lpar;with four arguments&rpar; is called&comma; which prints the sum of <strong>four<&sol;strong> numbers&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling one Secondary constructor from another<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>A secondary constructor may call another secondary constructor of the same class using <span style&equals;"color&colon; &num;008000&semi;"><strong>this <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">keyword<&sol;span><&sol;span>&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example <&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; Addition&lpar;2&comma; 3&rpar;&NewLine;&rcub;&NewLine;&NewLine;class Addition &lbrace;&NewLine; constructor&lpar;a&colon; Int&comma; b&colon; Int&rpar; &colon; this&lpar;a&comma; b&comma; 4&rpar; &lbrace;&NewLine; var sumOfTwo &equals; a &plus; b&NewLine; println&lpar;"The Sum of two numbers 2&comma; 3 is&colon; &dollar;&lbrace;sumOfTwo&rcub;"&rpar;&NewLine; &rcub;&NewLine;&NewLine; constructor&lpar;a&colon; Int&comma; b&colon; Int&comma; c&colon; Int&rpar; &lbrace;&NewLine; var sumOfThree &equals; a &plus; b &plus; c&NewLine; println&lpar;"The Sum of three numbers 2&comma; 3&comma; 4 is&colon; &dollar;&lbrace;sumOfThree&rcub;"&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>The Sum of three numbers 2&comma; 3&comma; 4 is&colon; 9&NewLine;The Sum of two numbers 2&comma; 3 is&colon; 5<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>Inside <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Addition<&sol;strong><&sol;span> class&comma; the secondary constructor &lpar;with two arguments&rpar; is calling the secondary constructor &lpar;with three arguments&rpar; using <span style&equals;"color&colon; &num;008000&semi;"><strong>this<&sol;strong><&sol;span> keyword&period;<&sol;li>&NewLine;<li>So&comma; the compiler will first execute secondary constructor &lpar;with three arguments&rpar; then secondary constructor &lpar;with two arguments&rpar;&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling parent class secondary constructor from child class secondary constructor <&sol;strong><&sol;span><&sol;h4>&NewLine;<p>We can call the secondary constructor of the parent class from the child class using the <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>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; Child&lpar;"Arun"&comma; "Verma"&rpar;&NewLine;&rcub;&NewLine;&NewLine;open class Parent &lbrace;&NewLine; constructor &lpar;pers&lowbar;fname&colon; String&comma; pers&lowbar;lname&colon; String&comma; pers&lowbar;age&colon; Int&rpar; &lbrace;&NewLine; var firstName&colon; String &equals; pers&lowbar;fname&NewLine; var lastName&colon; String &equals; pers&lowbar;lname&NewLine; var age&colon; Int &equals; pers&lowbar;age&NewLine; print&lpar;"FirstName &colon; &dollar;firstName&comma; "&rpar;&NewLine; print&lpar;"LastName&colon; &dollar;lastName&comma; "&rpar;&NewLine; println&lpar;"Age&colon; &dollar;age"&rpar;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Child &colon; Parent &lbrace;&NewLine; constructor &lpar;pers&lowbar;fname&colon; String&comma; pers&lowbar;lname&colon; String&rpar; &colon; <span style&equals;"color&colon; &num;008000&semi;"><strong>super&lpar;pers&lowbar;fname&comma; pers&lowbar;lname&comma; 26&rpar;<&sol;strong><&sol;span> &lbrace;&NewLine; var firstName&colon; String &equals; pers&lowbar;fname&NewLine; var lastName&colon; String &equals; pers&lowbar;lname&NewLine; print&lpar;"FirstName&colon; &dollar;firstName&comma; "&rpar;&NewLine; println&lpar;"LastName&colon; &dollar;lastName"&rpar;&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>FirstName &colon; Arun&comma; LastName&colon; Verma&comma; Age&colon; 26&NewLine;FirstName&colon; Arun&comma; LastName&colon; Verma<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>Inside <strong><span style&equals;"color&colon; &num;0000ff&semi;">Child<&sol;span><&sol;strong> class&comma; secondary constructor &lpar;with two arguments&rpar; is calling <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Parent<&sol;strong><&sol;span> class secondary constructor &lpar;with three arguments&rpar; using <span style&equals;"color&colon; &num;008000&semi;"><strong>super<&sol;strong><&sol;span> keyword&period;<&sol;li>&NewLine;<li>So&comma; the compiler will first execute Parent class secondary constructor then Child class secondary constructor&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version