Kotlin Extension Function

In this tutorial you will learn about the Kotlin Extension Function and its application with practical example.

Kotlin Extension Function

In Kotlin We can use extension function to extend class. In Most of the programming langauge we have to create derived class to do this. The Extension Function is declared outside the class and is a member function of class.

Example: Let’s get understand it by an example, In below example we are going to remove first two word from string. Check below code

In aboce code removeFirstTwoChar() is an extension function which is added to the String class.
String class is receiver type and the this keyword inside the extension function is receiver object.

 

Output: So here is the output.

ext-fun-1

 

In this tutorial we have learn about the Kotlin Extension Function and its application with practical example. I hope you will like this tutorial.