Site icon C1CTech

Kotlin Extension Function

<p>In this article&comma; you will learn to extend a class with new functionality using extension functions&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Extension Function<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Kotlin provides the ability to add methods to the existing classes without having to inherit from the class or use design patterns such as Decorator to do this&period;<&sol;p>&NewLine;<p>This is achieved using special declarations called <span style&equals;"color&colon; &num;008000&semi;"><strong>extensions<&sol;strong><&sol;span>&period; Using extensions when a function is added to an existing class&comma; it belongs to that class not internally but externally known as <span style&equals;"color&colon; &num;008000&semi;"><strong>Extension Function<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>The created extension functions are available for calling in the usual way as if they were methods of the original class&period;<&sol;p>&NewLine;<p>To declare an extension function&comma; we need to prefix its name with a <span style&equals;"color&colon; &num;008000&semi;"><strong>receiver type<&sol;strong><&sol;span>&comma; i&period;e&period; the type being extended or the class name&period;<&sol;p>&NewLine;<pre><strong>fun &lt&semi;class&lowbar;name&gt&semi;&period;&lt&semi;method&lowbar;name&gt&semi;&lpar;&rpar;<&sol;strong><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class Person &lbrace;&NewLine; var skills&colon; String&quest; &equals; null&NewLine;&NewLine; fun show&lpar;&rpar; &lbrace;&NewLine; println&lpar;skills&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; var p1 &equals; Person&lpar;&rpar;&NewLine; p1&period;skills &equals; "JAVA"&NewLine; p1&period;show&lpar;&rpar;&NewLine; var p2 &equals; Person&lpar;&rpar;&NewLine; p2&period;skills &equals; "KOTLIN"&NewLine; p2&period;show&lpar;&rpar;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;p3 has both skills&NewLine;<&sol;strong><&sol;span> <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;calling extension function add&lpar;&rpar;<&sol;span><&sol;strong>&NewLine; var p3 &equals; <strong><span style&equals;"color&colon; &num;008000&semi;">p1&period;add&lpar;p2&rpar;<&sol;span><&sol;strong>&NewLine; p3&period;show&lpar;&rpar;&NewLine;&rcub;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&NewLine;&sol;&sol;definition of extension func add&lpar;&rpar;<&sol;strong><&sol;span>&NewLine;<strong>fun <span style&equals;"color&colon; &num;008000&semi;">Person&period;add<&sol;span>&lpar;person&colon; Person&rpar;&colon; Person &lbrace;<&sol;strong>&NewLine;<strong> var p &equals; Person&lpar;&rpar;<&sol;strong>&NewLine;<strong> p&period;skills &equals; <span style&equals;"color&colon; &num;0000ff&semi;">this<&sol;span>&period;skills &plus; " &comma; " &plus; person&period;skills<&sol;strong>&NewLine;<strong> return p<&sol;strong>&NewLine;<strong>&rcub;<&sol;strong><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output <&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>JAVA &NewLine;KOTLIN &NewLine;JAVA &comma; KOTLIN<&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>The declare <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Person&period;add&lpar;&rpar;<&sol;strong> <&sol;span>function is an <span style&equals;"color&colon; &num;008000&semi;"><strong>extension<&sol;strong><&sol;span> function&comma; where class name &lpar;Person&rpar; is known as receiver type&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">this<&sol;span><&sol;strong> keyword inside the extension function refers the receiver object &lpar;p1&rpar;&period;<&sol;li>&NewLine;<li>The <span style&equals;"color&colon; &num;008000&semi;"><strong>add&lpar;&rpar;<&sol;strong><&sol;span> function will concatenate the skills&lpar;property&rpar; of  two object of Person type and return an object of Person type &lpar;having both skills&rpar;&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>In case we want to define an extension function which has the same name and signature as that of a member function present inside the receiver class&comma; then always the member function will execute when we make a call to the function&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class Example &lbrace;&NewLine; fun printFunctionType&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Class method"&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun Example&period;printFunctionType&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Extension function"&rpar;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;member function always wins<&sol;strong><&sol;span>&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&lpar;&rpar;&period;printFunctionType&lpar;&rpar;<&sol;strong><&sol;span>&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 method<&sol;pre>&NewLine;<p>However&comma; it&&num;8217&semi;s perfectly OK for extension functions to overload member functions which have the same name but a different signature&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class Example &lbrace;&NewLine; fun printFunctionType&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Class method"&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;public fun Example&period;printFunctionType&lpar;i&colon; Int&rpar; &lbrace;&NewLine; println&lpar;"Extension function"&rpar;&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;0000ff&semi;"> Example&lpar;&rpar;&period;printFunctionType&lpar;1&rpar;<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>Extension method<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Extended library class using extension function <&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Kotlin not only allows the user-defined classes to be extended but also the library classes can be extended&period; The extension function can be added to library classes and used in a similar way as for user-defined classes&period;<&sol;p>&NewLine;<p>The following example demonstrates an extension function created for a built-in class <span style&equals;"color&colon; &num;008000&semi;"><strong>String<&sol;strong><&sol;span>&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><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;extending built-in class String<&sol;strong><&sol;span>&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; val str1 &equals; "Hello "&NewLine; val str2 &equals; "World"&NewLine; val str3 &equals; "Hey "&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;calling extension function add&lpar;&rpar;<&sol;strong><&sol;span>&NewLine; val result &equals; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>str3&period;add&lpar;str1&comma; str2&rpar;<&sol;strong><&sol;span>&NewLine; println&lpar;"The new string is&colon; &dollar;result"&rpar;&NewLine;&rcub;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;definition of extension function add&lpar;&rpar;<&sol;strong><&sol;span>&NewLine;fun String&period;add&lpar;s1&colon; String&comma; s2&colon; String&rpar;&colon; String &lbrace;&NewLine; var newStr &equals; this &plus; s1 &plus; s2&NewLine; return newStr&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 new string is&colon; Hey Hello World<&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>The declare <span style&equals;"color&colon; &num;0000ff&semi;"><strong>String&period;add&lpar;&rpar;<&sol;strong> <&sol;span>function is an <span style&equals;"color&colon; &num;008000&semi;"><strong>extension<&sol;strong><&sol;span> function&comma; where class name &lpar;String&rpar; is the receiver type&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">this<&sol;span><&sol;strong> keyword inside the extension function refers the receiver object &lpar;str3&rpar;&period;<&sol;li>&NewLine;<li>the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>add&lpar;&rpar;<&sol;strong><&sol;span> function will concatenate the two string values &lpar;passed during call&rpar; with the str3 value &lpar;receiver object&rpar; and return a value of String type&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Nullable Reciever <&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Extension functions can also be defined with the class type that is <span style&equals;"color&colon; &num;008000&semi;"><strong>nullable<&sol;strong><&sol;span>&period; In this case&comma; the nullability of object is checked using <span style&equals;"color&colon; &num;008000&semi;"><strong>this &equals;&equals; null<&sol;strong><&sol;span> inside the body&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class Person &lbrace;&NewLine; var skills&colon; String&quest; &equals; null&NewLine;&NewLine; fun show&lpar;&rpar; &lbrace;&NewLine; println&lpar;skills&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; var p1&colon; Person&quest; &equals; Person&lpar;&rpar;&NewLine; p1&quest;&period;skills &equals; "KOTLIN"&NewLine; p1&period;output&lpar;&rpar;&NewLine; p1 &equals; null&NewLine; p1&period;output&lpar;&rpar;&NewLine;&rcub;&NewLine;&NewLine;fun Person&quest;&period;output&lpar;&rpar; &lbrace;&NewLine; if &lpar;this &equals;&equals; null&rpar; &lbrace;&NewLine; println&lpar;"Null"&rpar;&NewLine; &rcub; else this&period;show&lpar;&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>KOTLIN&NewLine;Null<&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>The declare <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Person&quest;&period;output&lpar;&rpar;<&sol;strong> <&sol;span>function is an <span style&equals;"color&colon; &num;008000&semi;"><strong>extension<&sol;strong><&sol;span> function&comma; where class name &lpar;Person&rpar; is the receiver type&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">this<&sol;span><&sol;strong> keyword inside the extension function refers the receiver object &lpar;p1&rpar;&period;<&sol;li>&NewLine;<li>When p1 is not null&comma; the else condition of <span style&equals;"color&colon; &num;008000&semi;"><strong>output&lpar;&rpar;<&sol;strong> <&sol;span>will execute which will print the skills property&period;<&sol;li>&NewLine;<li>When p1 is null&comma; if condition of <strong><span style&equals;"color&colon; &num;008000&semi;">output&lpar;&rpar;<&sol;span><&sol;strong> will execute which will print <strong><span style&equals;"color&colon; &num;008000&semi;">Null<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Companion Object Extensions <&sol;strong><&sol;span><&sol;h4>&NewLine;<p>A companion object is an object which is declared inside a class and marked with the <span style&equals;"color&colon; &num;008000&semi;"><strong>companion<&sol;strong><&sol;span> keyword&period; Companion object is used to call the member function of class directly using the class name &lpar;like static in java&rpar;&period;<&sol;p>&NewLine;<p>If a class contain companion object&comma; then we can also define extension functions and properties for the companion object&period;<&sol;p>&NewLine;<p>Just like the calling of regular member function of the companion object&comma; extension function can be called using only the class name as the qualifier&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>class MyClass &lbrace;&NewLine; companion object &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Member function of companion object<&sol;strong><&sol;span>&NewLine; fun display&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Member function of companion object"&rpar;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Extension function of companion object<&sol;span><&sol;strong>&NewLine;fun <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyClass&period;Companion&period;output&lpar;&rpar;<&sol;strong><&sol;span> &lbrace;&NewLine; println&lpar;"Extension function of companion object"&rpar;&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;calling member function of companion object<&sol;span><&sol;strong>&NewLine; MyClass&period;display&lpar;&rpar;&NewLine;&NewLine; <strong> <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;calling extension function of companion object<&sol;span><&sol;strong>&NewLine; MyClass&period;output&lpar;&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>Member function of companion object&NewLine;Extension function of companion object<&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>The declare <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyClass&period;Companion&period;output&lpar;&rpar;<&sol;strong> <&sol;span>function is an <span style&equals;"color&colon; &num;008000&semi;"><strong>extension<&sol;strong><&sol;span> function of <strong><span style&equals;"color&colon; &num;008000&semi;">companion<&sol;span><&sol;strong> object of <span style&equals;"color&colon; &num;008000&semi;"><strong>MyClass<&sol;strong><&sol;span>&comma; where class name &lpar;<strong><span style&equals;"color&colon; &num;008000&semi;">MyClass<&sol;span><&sol;strong>&rpar; is the receiver type&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyClass&period;display&lpar;&rpar;<&sol;strong><&sol;span> will call member function <span style&equals;"color&colon; &num;008000&semi;"><strong>display&lpar;&rpar;<&sol;strong><&sol;span> of companion object&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyClass&period;output&lpar;&rpar;<&sol;strong><&sol;span> will call extension function<span style&equals;"color&colon; &num;008000&semi;"><strong> output&lpar;&rpar;<&sol;strong><&sol;span> of companion object&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version