Site icon C1CTech

Kotlin Inheritance

<p>In this tutorial&comma; we will learn about Inheritance in Kotlin with the help of examples&period;<&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><strong>Inheritance<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Inheritance <&sol;b><&sol;span>is one of the key feature of <b><span style&equals;"color&colon; &num;0000ff&semi;">OOP<&sol;span> &lpar;Object Oriented Programming&rpar;<&sol;b>&period; <b><span style&equals;"color&colon; &num;008000&semi;">Inheritance<&sol;span> <&sol;b>can be defined as the process where one class is allowed to inherit the features &lpar;properties and functions&rpar; of another class&period;<&sol;p>&NewLine;<p class&equals;"p1">The class which inherits the features of other class is known as <b><span style&equals;"color&colon; &num;0000ff&semi;">sub class<&sol;span> <&sol;b>&lpar;or a derived class or a child class&rpar;&period;<&sol;p>&NewLine;<p class&equals;"p1">The class whose features are inherited is known as <span style&equals;"color&colon; &num;0000ff&semi;"><b>super class<&sol;b><&sol;span> &lpar; or a base class or  a parent class&rpar;&period;<&sol;p>&NewLine;<p>Inheritance supports the concept of <span style&equals;"color&colon; &num;0000ff&semi;">&OpenCurlyDoubleQuote;<b>reusability<&sol;b>”<&sol;span>&comma; i&period;e&period; the code that is present in the parent class doesn’t need to be written again in the child class&period;<&sol;p>&NewLine;<p class&equals;"p1">The <span style&equals;"color&colon; &num;008000&semi;"><strong>subclass<&sol;strong><&sol;span> inherits all members that exist in its <span style&equals;"color&colon; &num;008000&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; and can add some members of their own&period;<&sol;p>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax of Inheritance<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre><span style&equals;"color&colon; &num;0000ff&semi;"><strong>open<&sol;strong><&sol;span> class Superclass &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;properties and functions<&sol;span><&sol;strong> &NewLine;&rcub;&NewLine;&NewLine;class Subclass<span style&equals;"color&colon; &num;0000ff&semi;"> <strong>&colon;<&sol;strong><&sol;span> Superclass&lpar;&rpar;&NewLine;&lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;properties and functions<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1">In Kotlin&comma;<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1">All classes are <span style&equals;"color&colon; &num;0000ff&semi;"><b>final<&sol;b><&sol;span>  by default &lpar;final class cannot be subclassed&rpar; &period; To inherit&comma; super class for sub class we should use <span style&equals;"color&colon; &num;0000ff&semi;"><b>open<&sol;b><&sol;span> keyword in front of super class&period;<&sol;li>&NewLine;<li class&equals;"p1">The sub class inherits a super class using <strong>&colon;<&sol;strong> operator in the class header &lpar;after the sub class name or constructor&rpar;&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1">When we inherit a class then all the properties and functions are also inherited&period; We can use the super class properties and functions in the sub class and can also call functions using the sub class object&period;<&sol;p>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Inheritance Example<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Calculator is a super class<&sol;span>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">open<&sol;span><&sol;strong> class <strong><span style&equals;"color&colon; &num;0000ff&semi;">Calculator<&sol;span><&sol;strong> &lbrace;&NewLine; var num &equals; 9&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Calculator class members<&sol;span><&sol;strong>&NewLine; fun add&lpar;x&colon; Int&comma; y&colon; Int&rpar;&colon; Int &lbrace;&NewLine; return x &plus; y&NewLine; &rcub;&NewLine;&NewLine; fun sub&lpar;x&colon; Int&comma; y&colon; Int&rpar;&colon; Int &lbrace;&NewLine; return x - y&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;sub class CalcAdv inherit Calculator<&sol;span><&sol;strong>&NewLine;class <strong><span style&equals;"color&colon; &num;0000ff&semi;">CalcAdv<&sol;span><&sol;strong> &colon; Calculator&lpar;&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;CalcAdv class members<&sol;strong><&sol;span>&NewLine; fun sqr&lpar;&rpar;&colon; Int &lbrace;&NewLine; return num &ast; num <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;inherit num property<&sol;strong><&sol;span>&NewLine; &rcub;&NewLine;&NewLine; fun sqrt&lpar;&rpar;&colon; Double &lbrace;&NewLine; return Math&period;sqrt&lpar;num&period;toDouble&lpar;&rpar;&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;inherit num property<&sol;strong><&sol;span>&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating CalcAdv instance<&sol;span><&sol;strong>&NewLine; val calcAdv &equals; CalcAdv&lpar;&rpar;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; calling add&lpar;&rpar; and sub&lpar;&rpar;&comma; which are inherited from Calculator<&sol;span><&sol;strong>&NewLine; println&lpar;"The sum of the given numbers&colon; " &plus; calcAdv&period;add&lpar;10&comma; 20&rpar;&rpar;&NewLine; println&lpar;"The difference between the given numbers&colon; " &plus; calcAdv&period;sub&lpar;30&comma; 20&rpar;&rpar;&NewLine;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; calling sqr&lpar;&rpar; and sqrt&lpar;&rpar;&comma; which are CalcAdv's own functions<&sol;span><&sol;strong>&NewLine; println&lpar;"The square of a given number&colon; " &plus; calcAdv&period;sqr&lpar;&rpar;&rpar;&NewLine; println&lpar;"The square root of a given number&colon; " &plus; calcAdv&period;sqrt&lpar;&rpar;&rpar;&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 the given numbers&colon; 30&NewLine;The difference between the given numbers&colon; 10&NewLine;The square of a given number&colon; 81&NewLine;The square root of a given number&colon; 3&period;0<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h5>&NewLine;<p>In the above example&comma;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2">We have inherited a subclass <span style&equals;"color&colon; &num;008000&semi;"><b>CalcAdv<&sol;b><&sol;span> from super class <span style&equals;"color&colon; &num;008000&semi;"><b>Calculator<&sol;b><&sol;span>&period;<&sol;li>&NewLine;<li class&equals;"li2">The CalcAdv class inherits the property <span style&equals;"color&colon; &num;008000&semi;"><strong>num<&sol;strong><&sol;span> and the functions <span style&equals;"color&colon; &num;008000&semi;"><b>add&lpar;&rpar;<&sol;b><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><b>sub&lpar;&rpar;<&sol;b><&sol;span> from the Calculator class&period;<&sol;li>&NewLine;<li class&equals;"li2">When an object of <span style&equals;"color&colon; &num;008000&semi;"><b>CalcAdv<&sol;b><&sol;span> class is created&comma; a copy of all functions and properties of the super class acquire memory in this object&period; That is why by using the object of the subclass we can also access the members of a super class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><strong>Overriding member functions and properties<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">Just like Kotlin classes&comma; members &lpar;properties and functions&rpar; of a Kotlin class are also <span style&equals;"color&colon; &num;0000ff&semi;"><strong>final<&sol;strong><&sol;span> by default&period; To allow a member function to be overridden &lpar;redefine or modify the method of its superclass into subclass&rpar;&comma; you need to mark it with the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>open<&sol;strong><&sol;span> modifier&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 class&equals;"p1">Moreover&comma; the sub class that overrides a super class function must use the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>override<&sol;strong><&sol;span> modifier&period;<&sol;p>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Kotlin program of overriding the member function&colon;<&sol;b><&sol;span><&sol;h5>&NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Employee is a super class<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>open<&sol;strong><&sol;span> class <span style&equals;"color&colon; &num;000000&semi;"><strong>Employee<&sol;strong><&sol;span> &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">open<&sol;span><&sol;strong> fun show&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;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;sub class Manager inherit Employee<&sol;strong><&sol;span>&NewLine;class <span style&equals;"color&colon; &num;000000&semi;"><strong>Manager<&sol;strong><&sol;span> &colon; Employee&lpar;&rpar; &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;overriding show&lpar;&rpar; to provide new implementation<&sol;strong><&sol;span>&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>override<&sol;strong><&sol;span> fun show&lpar;name&colon; String&comma; age&colon; Int&rpar; &lbrace;&NewLine; println&lpar;"My name is &dollar;name "&rpar;&NewLine; println&lpar;"I am Manager in HCL company"&rpar;&NewLine; &rcub;&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 Manager instance<&sol;span><&sol;strong>&NewLine; val manager &equals; Manager&lpar;&rpar;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;show&lpar;&rpar; of Manager class will call<&sol;span><&sol;strong>&NewLine; manager&period;show&lpar;"Arun"&comma; 25&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 &NewLine;I am Manager in HCL company<&sol;pre>&NewLine;<p class&equals;"p1">Similarly&comma; we can override the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>property<&sol;strong><&sol;span> of super class in sub class&period;<&sol;p>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Kotlin program of overriding the member property &colon;<&sol;b><&sol;span><&sol;h5>&NewLine;<pre><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Employee is a super class<&sol;span>&NewLine;open<&sol;strong><&sol;span> class <span style&equals;"color&colon; &num;000000&semi;"><strong>Employee<&sol;strong><&sol;span> &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">open<&sol;span><&sol;strong> var position&colon; String &equals; "Worker"&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>open<&sol;strong><&sol;span> var salary &equals; "20000"&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; sub class Manager inherit Employee<&sol;span><&sol;strong>&NewLine;class <strong><span style&equals;"color&colon; &num;000000&semi;">Manager<&sol;span><&sol;strong> &colon; Employee&lpar;&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;overriding properties to set new values<&sol;span><&sol;strong>&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">override<&sol;span><&sol;strong> var position&colon; String &equals; "Manager"&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">override<&sol;span><&sol;strong> var salary &equals; "70000"&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 Manager instance<&sol;span><&sol;strong>&NewLine; val m &equals; Manager&lpar;&rpar;&NewLine; println&lpar;"I am " &plus; <strong>m&period;position<&sol;strong> &plus; " and earning " &plus; <strong>m&period;salary<&sol;strong> &plus; " per month&period;"&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>I am Manager and earning 70000 per month&period;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling properties and functions of superclass <&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">When you override a property or a member function of a super class&comma; the super class implementation is shadowed by the child class implementation&period;<&sol;p>&NewLine;<p class&equals;"p1">You can access super class member functions or properties from the sub class using the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>super<&sol;strong><&sol;span> keyword&period;<&sol;p>&NewLine;<p class&equals;"p1">In the below program we have called the super class property <span style&equals;"color&colon; &num;008000&semi;"><strong>color<&sol;strong><&sol;span> and function <span style&equals;"color&colon; &num;008000&semi;"><strong>draw&lpar;&rpar;<&sol;strong><&sol;span> in sub class using the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>super<&sol;strong><&sol;span> keyword&period;<&sol;p>&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;Shape is a super class<&sol;span>&NewLine;open<&sol;strong><&sol;span> class Shape &lbrace;&NewLine;&NewLine; val color&colon; String &equals; "blue"&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">open<&sol;span><&sol;strong> fun draw&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Drawing a shape&period;&period;"&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;sub class Rectangle inherit Shape<&sol;span><&sol;strong> &NewLine;class Rectangle &colon; Shape&lpar;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">override<&sol;span><&sol;strong> fun draw&lpar;&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;calling Shape class draw&lpar;&rpar; and &NewLine; &sol;&sol;color property using super keyword<&sol;span><&sol;strong>&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">super&period;draw&lpar;&rpar;<&sol;span><&sol;strong>&NewLine; println&lpar;"Filling the rectangle with " &plus; <strong><span style&equals;"color&colon; &num;0000ff&semi;">super&period;color<&sol;span><&sol;strong> &plus; " color"&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 Rectangle instance<&sol;span><&sol;strong>&NewLine; val rectangle &equals; Rectangle&lpar;&rpar;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;calling Rectangle class draw&lpar;&rpar;<&sol;strong><&sol;span>&NewLine; rectangle&period;draw&lpar;&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>Drawing a shape&period;&period;&NewLine;Filling the rectangle with blue color<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version