<p>In this article, you will learn to extend a class with new functionality using extension functions.</p>
<h4><span style="color: #000080;"><strong>Extension Function</strong></span></h4>
<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.</p>
<p>This is achieved using special declarations called <span style="color: #008000;"><strong>extensions</strong></span>. Using extensions when a function is added to an existing class, it belongs to that class not internally but externally known as <span style="color: #008000;"><strong>Extension Function</strong></span>.</p>
<p>The created extension functions are available for calling in the usual way as if they were methods of the original class.</p>
<p>To declare an extension function, we need to prefix its name with a <span style="color: #008000;"><strong>receiver type</strong></span>, i.e. the type being extended or the class name.</p>
<pre><strong>fun <;class_name>;.<;method_name>;()</strong></pre>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class Person {
 var skills: String? = null

 fun show() {
 println(skills)
 }
}

fun main(args: Array<;String>;) {
 var p1 = Person()
 p1.skills = "JAVA"
 p1.show()
 var p2 = Person()
 p2.skills = "KOTLIN"
 p2.show()

 <span style="color: #008000;"><strong>//p3 has both skills
</strong></span> <strong><span style="color: #008000;"> //calling extension function add()</span></strong>
 var p3 = <strong><span style="color: #008000;">p1.add(p2)</span></strong>
 p3.show()
}
<span style="color: #008000;"><strong>
//definition of extension func add()</strong></span>
<strong>fun <span style="color: #008000;">Person.add</span>(person: Person): Person {</strong>
<strong> var p = Person()</strong>
<strong> p.skills = <span style="color: #0000ff;">this</span>.skills + " , " + person.skills</strong>
<strong> return p</strong>
<strong>}</strong></pre>
<h5><span style="color: #0000ff;"><strong>Output </strong></span></h5>
<pre>JAVA 
KOTLIN 
JAVA , KOTLIN</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>The declare <span style="color: #0000ff;"><strong>Person.add()</strong> </span>function is an <span style="color: #008000;"><strong>extension</strong></span> function, where class name (Person) is known as receiver type.</li>
<li>The <strong><span style="color: #0000ff;">this</span></strong> keyword inside the extension function refers the receiver object (p1).</li>
<li>The <span style="color: #008000;"><strong>add()</strong></span> function will concatenate the skills(property) of two object of Person type and return an object of Person type (having both skills).</li>
</ul>
<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, then always the member function will execute when we make a call to the function.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class Example {
 fun printFunctionType() {
 println("Class method")
 }
}

fun Example.printFunctionType() {
 println("Extension function")
}

fun main(args: Array<;String>;) {
<span style="color: #008000;"><strong>//member function always wins</strong></span>
 <span style="color: #0000ff;"><strong>Example().printFunctionType()</strong></span>
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Class method</pre>
<p>However, it&#8217;s perfectly OK for extension functions to overload member functions which have the same name but a different signature.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class Example {
 fun printFunctionType() {
 println("Class method")
 }
}

public fun Example.printFunctionType(i: Int) {
 println("Extension function")
}

fun main(args: Array<;String>;) {
<strong><span style="color: #0000ff;"> Example().printFunctionType(1)</span></strong>
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Extension method</pre>
<h4></h4>
<h4><span style="color: #000080;"><strong>Extended library class using extension function </strong></span></h4>
<p>Kotlin not only allows the user-defined classes to be extended but also the library classes can be extended. The extension function can be added to library classes and used in a similar way as for user-defined classes.</p>
<p>The following example demonstrates an extension function created for a built-in class <span style="color: #008000;"><strong>String</strong></span>.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre><span style="color: #008000;"><strong>//extending built-in class String</strong></span>
fun main(args: Array<;String>;) {
 val str1 = "Hello "
 val str2 = "World"
 val str3 = "Hey "

 <span style="color: #008000;"><strong>//calling extension function add()</strong></span>
 val result = <span style="color: #0000ff;"><strong>str3.add(str1, str2)</strong></span>
 println("The new string is: $result")
}

<span style="color: #008000;"><strong>//definition of extension function add()</strong></span>
fun String.add(s1: String, s2: String): String {
 var newStr = this + s1 + s2
 return newStr
}</pre>
<h5><span style="color: #0000ff;"><strong>Output </strong></span></h5>
<pre>The new string is: Hey Hello World</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>The declare <span style="color: #0000ff;"><strong>String.add()</strong> </span>function is an <span style="color: #008000;"><strong>extension</strong></span> function, where class name (String) is the receiver type.</li>
<li>The <strong><span style="color: #0000ff;">this</span></strong> keyword inside the extension function refers the receiver object (str3).</li>
<li>the <span style="color: #0000ff;"><strong>add()</strong></span> function will concatenate the two string values (passed during call) with the str3 value (receiver object) and return a value of String type.</li>
</ul>
<h4></h4>
<h4><span style="color: #000080;"><strong>Nullable Reciever </strong></span></h4>
<p>Extension functions can also be defined with the class type that is <span style="color: #008000;"><strong>nullable</strong></span>. In this case, the nullability of object is checked using <span style="color: #008000;"><strong>this == null</strong></span> inside the body.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class Person {
 var skills: String? = null

 fun show() {
 println(skills)
 }
}

fun main(args: Array<;String>;) {
 var p1: Person? = Person()
 p1?.skills = "KOTLIN"
 p1.output()
 p1 = null
 p1.output()
}

fun Person?.output() {
 if (this == null) {
 println("Null")
 } else this.show()
}</pre>
<h5><span style="color: #0000ff;"><strong>Output </strong></span></h5>
<pre>KOTLIN
Null</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>The declare <span style="color: #0000ff;"><strong>Person?.output()</strong> </span>function is an <span style="color: #008000;"><strong>extension</strong></span> function, where class name (Person) is the receiver type.</li>
<li>The <strong><span style="color: #0000ff;">this</span></strong> keyword inside the extension function refers the receiver object (p1).</li>
<li>When p1 is not null, the else condition of <span style="color: #008000;"><strong>output()</strong> </span>will execute which will print the skills property.</li>
<li>When p1 is null, if condition of <strong><span style="color: #008000;">output()</span></strong> will execute which will print <strong><span style="color: #008000;">Null</span></strong>.</li>
</ul>
<h4></h4>
<h4><span style="color: #000080;"><strong>Companion Object Extensions </strong></span></h4>
<p>A companion object is an object which is declared inside a class and marked with the <span style="color: #008000;"><strong>companion</strong></span> keyword. Companion object is used to call the member function of class directly using the class name (like static in java).</p>
<p>If a class contain companion object, then we can also define extension functions and properties for the companion object.</p>
<p>Just like the calling of regular member function of the companion object, extension function can be called using only the class name as the qualifier.</p>
<h5><span style="color: #0000ff;"><strong>Example</strong></span></h5>
<pre>class MyClass {
 companion object {
 <span style="color: #008000;"><strong>// Member function of companion object</strong></span>
 fun display() {
 println("Member function of companion object")
 }
 }
}

<strong><span style="color: #008000;">// Extension function of companion object</span></strong>
fun <span style="color: #0000ff;"><strong>MyClass.Companion.output()</strong></span> {
 println("Extension function of companion object")
}

fun main(args: Array<;String>;) {
 <strong> <span style="color: #008000;">//calling member function of companion object</span></strong>
 MyClass.display()

 <strong> <span style="color: #008000;">//calling extension function of companion object</span></strong>
 MyClass.output()
}</pre>
<h5><span style="color: #0000ff;"><strong>Output </strong></span></h5>
<pre>Member function of companion object
Extension function of companion object</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>The declare <span style="color: #0000ff;"><strong>MyClass.Companion.output()</strong> </span>function is an <span style="color: #008000;"><strong>extension</strong></span> function of <strong><span style="color: #008000;">companion</span></strong> object of <span style="color: #008000;"><strong>MyClass</strong></span>, where class name (<strong><span style="color: #008000;">MyClass</span></strong>) is the receiver type.</li>
<li><span style="color: #0000ff;"><strong>MyClass.display()</strong></span> will call member function <span style="color: #008000;"><strong>display()</strong></span> of companion object.</li>
<li><span style="color: #0000ff;"><strong>MyClass.output()</strong></span> will call extension function<span style="color: #008000;"><strong> output()</strong></span> of companion object.</li>
</ul>
<p> ;</p>


