Extension function in Kotlin
- Authors
- Name
- Amit Shekhar
- Published on
An extension function in Kotlin lets us add new functions to existing classes without modifying their source code or extending them.
In this blog, we will learn about the extension function in Kotlin. We will also learn how it works internally.
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning, and Android at Outcome School.
Join Outcome School and get a high-paying tech job:
Let's get started.
What is an extension function in Kotlin?
An extension function in Kotlin allows us to add new functions to existing classes without modifying their source code or extending them. This is particularly useful when we want to extend a class with new capabilities but don't have access to edit its code or don't want to extend it.
Let's understand it with an example.
Syntax to create an extension function:
fun ClassName.functionName(parameters): ReturnType {
// function body
}
Now, we will try to understand the above syntax.
- ClassName: The class we are extending.
- functionName: The name of the extension function.
- parameters: Parameters that the extension function takes.
- ReturnType: The return type of the extension function.
Let's write an extension function using the above syntax in a file named: extension.kt.
fun ImageView.loadImage(url: String) {
Glide.with(this).load(url).into(this)
}
Inside the above extension function, imagine we have extended the ImageView class, so, this represents ImageView Object.
Using the above extension function:
imageView.loadImage("someUrl")
If you notice, we added a function to the ImageView without modifying its source code or extending it.
The best part is that, even though it’s defined outside the class it extends, the function behaves as if it were a member of that class. It’s called just like any other class method, making the syntax seamless and natural.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
How does it work internally?
When we call an extension function on an object, the compiler passes the object as the first argument to the function.
Now, we need to decompile this code. For that, we will have to convert this Kotlin source file to a Java source file.
Steps to convert from Kotlin source file to Java source file and decompile in Android Studio:
Tools>Kotlin>Show Kotlin Bytecode. You will get the bytecode of your Kotlin file.- Now click on the
Decompilebutton to get your Java code from the bytecode.
Here, I have kept only the important lines of the code and removed other lines for brevity.
We will get the following output:
public final class ExtensionKt {
public static final void loadImage(ImageView imageView, String url) {
Glide.with(imageView).load(url).into(imageView);
}
}
Here, a final class has been generated with the static function loadImage, and most importantly, imageView is passed as the first argument to the function.
This is how an extension function works internally.
A final note:
- Extension functions are defined outside the class they extend.
- They can access the public members of the extended class.
- They cannot access the class's private or protected members.
- They do not modify the actual class but appear to add new methods.
Hence, Extension functions are useful for adding functionality to classes from libraries or the standard library that we can't modify directly.
Now, we have understood the extension function in Kotlin.
To learn Kotlin features like extension functions, higher-order functions, infix notation, and inline functions in depth, check out the Android Program by Outcome School.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Frequently Asked Questions
Can an extension function access private members of a class?
No. An extension function can access only the public members of the class it extends. It cannot access the class's private or protected members.
Does an extension function change the original class?
No. An extension function does not modify the actual class. It only appears to add a new method. We do not change the source code of the class or extend it, but we can still call the function just like any other method of that class.
When should we use an extension function in Kotlin?
We should use an extension function when we want to add new capabilities to a class that we cannot or do not want to modify. It is especially useful for classes from libraries or the standard library. For example, we can add a loadImage function to ImageView without touching its code.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
