Kotlin extension function with example

Shirsh Shukla
4 min readFeb 26, 2022

A Kotlin programmer can extend a class without inheriting its functionality.

Hello Friend, In this article, we will discuss the Kotlin extension function.
Kotlin extension functions allow a developer to “add” methods to a class without inheriting it or using a design pattern. The created extensions are used as regular functions within the class. Extension functions are declared with a prefix receiver type and method name.

To extend a class or implement an interface in Kotlin, we use a single colon (“:”) rather than the Java extends keyword. We can then create an instance of type Programmer and call methods on it, either in the class, it belongs to or in the base class.

The purpose of the extension function is to add a new function to the existing class so it simply adds a particular function to a predefined class without declaring it inside the class and the new functions actually behave just like the static functions.

So, let us now check out the extension function work with some simple Examples,

on this above example, you can check the status of your age, because this defines inside of our class AgesStage.

but what happens if we could need another function, that check is am a baby child or not, but that’s not defined inside the class, at this point what we do.
You think we have only one option that needs to create another function inside AgesStage class.

Now at this point, we need the Concept of Kotlin Extension, So what we do… without defining the function right inside the class what you can do is you can simply create one more function, before the name of another function we just added class name and dot, like this

So like these ways you can also create function.

If I tell you that this is actually not the practical usage of the extension function so whatever we saw right now was just an explanation of the extension function how do we define it the syntax is like this now let us come to the point let us define a function and check out the practical usage of the extension function so let us now change the code.
For Example, if I need to String Concat(add multiple strings into one)
So what I will do

but, this solution looks bulky and annoying to do, so what we will do.
we create an extension function for that,

So by this type, you can create your own String function and call as your own way.
Now let's try this for Integer, we can create a function to check greater value.

in the above example, I have created a function that checks greater value between two integers. and this function I will just clear some points,
this refer to x variable and otherValue refer to y variable.

so this is the way you can use the extension function in the case of Kotlin now what are the advantages of using the extension function.

let’s check it out so the extension function has few properties,
Such as they can become part of your own class example as we create AgesStage class.
and the other thing is that they can become part of the predefined classes such as array string integer care or any other predefined classes that come with the Kotlin SDK right so always remember this you can make it the part of your custom class such as AgesStage that we saw in above and also for the predefined classes such as string int and so on and now what are the advantages of using the extension functions it simply reduces the code and it makes the code much more cleaner and easy to read so it simply reduces code by allowing you to add your own custom methods inside some class so you don’t have to modify your old classes or the predefined classes for just adding one function.

also, I want to define one more thing, which is the Infix function. so let's first discuss what is infix function, so first more informational point is all the Infix function is the Extension function but all Extension function is not the Infix function. Also, the Infix function should have only defined one parameter. And last but not least point is all Infix functions should start with the prefix name as an infix. but…
why do we need to know the infix function? let's see by example.

as you see above we call a function this way x greaterValue y, not this x.greaterValue(y), by using infix function you don’t need to code that you call the method by dot(“.”) also which parameter you define inside in bracket“(), just add space between there infix function automatically detects you requirement
because by using the infix function you got some additional functionality as compared to the normal extension function, they have only one less functionality is we can’t call this method by two parameters.

So that’s all the things that I want to describe for the Kotlin extension function in a small manner. if you want to something more information please comment below.

If you got something wrong? Mention it in the comments. I would love to improve. your support means a lot to me! If you enjoy the content, I’d be grateful if you could consider subscribing to my YouTube channel as well.

I am Shirsh Shukla, a creative Developer, and a Technology lover. You can find me on LinkedIn or maybe follow me on Twitter or just walk over my portfolio for more details. And of course, you can follow me on GitHub as well.

Have a nice day!🙂

--

--

Shirsh Shukla

SDE at Reliance Jio | Mobile Application Developer | Speaker | Technical Writer | community member at Stack Overflow | Organizer @FlutterIndore