Extension function in Kotlin

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
Extension function in Kotlin

I am Amit Shekhar, Co-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.

Join Outcome School and get high paying tech job: Outcome School

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

In this blog, we will learn about the extension function in Kotlin. We will also learn how it works internally.

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.

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 Decompile button 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.

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar
Co-Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.