Affiliate links on Android Authority may earn us a commission.Learn more.
Adding new functionality with Kotlin’s extension functions
August 04, 2025
Is there a Java class that you’ve always felt was missing some useful functionality for Android development? With Kotlin it is possible to quickly and easily add functionality to existing classes, thanks to its extension functions. Here’s how to customize Kotlin and Java classes so that they provide exactly the functionality your project requires, including closed classes that were previously impossible to modify.
Read Next:Introduction to Kotlin for Android
What are extension functions?
Kotlin’s extension functions provide you with a way of “adding” methods to a class, without having to inherit from that class or use any type of design pattern. Once you’ve created an extension function, you can use it just like any other regularly defined function inside that class.
Read Next:Simplify asynchronous programming with Kotlin’s coroutines

Extension functions have the potential to make your code more concise, readable, and logical by trimming boilerplate code from your project. Less code also means fewer opportunities for errors. For example, you’re far less likely to slip up when writing the extension function:
Compared to:
Note that even though extension functions are commonly discussed in terms of “modifying” or “adding” functionality to an existing class, they don’t actually insert any new members into the class you’re extending. Under the hood, extension functions are resolved statically, so when you define an extension function you’re actually making a new function callable on variables of this type.
Creating an extension function
You can define extension functions anywhere in your project, although to help keep everything organized you may want to place them inside a dedicated file. This approach can also help you re-use extension functions, with this file acting as a library of helper functions to be copied and pasted across multiple projects. Throughout this article, I’ll be defining all my extension functions inside an extensions.kt file.
To create an extension function, write the name of the class or the type that you want to extend (known as the receiver type), followed by the dot notation (.) and the name of the function you want to create. you’re able to then write the function as normal.

Let’s look at how you’d create an extension function that lets you create a toast in much less code. By default, you need to write the following to display a toast:
Let’s move this code into an extension function, by extending Context with a ‘toast’ function:

The ‘this’ keyword inside the extension function body references the receiver object, which is the instance that you’re calling the extension function on (i.e whatever’s passed before the dot notation).
Then, simply import this extension function at the call site and you’re ready to use ‘toast’ just like any other function:
Note that I’m using Kotlin Android Extensions to import references to the Button and TextView UI elements into the Kotlin source file, which is why there’s no findViewByIds in the above code.
Android Studio also takes your extension functions into account when offering suggestions. Once you’ve defined a ‘toast’ function, Android Studio will suggest that you invoke the toast extension function whenever you’re inside Context or an instance of Context.
you may define extension functions for any class missing functionality which you want to use in your project. For example, if you’ve always wished View contained ‘short’ and ‘hide’ methods, you can implement them as extension functions:
Another common example is creating extension functions that take the pain out of formatting large amounts of text. Here we’re creating an extension function that capitalizes the first letter of every String:
A big part of Kotlin’s appeal is that it’s 100 percent interoperable with Java. This makes it possible to introduce Kotlin into your existing code bases without having to immediately convert all your existing Java code to Kotlin.
To preserve compatibility with Java, all extension functions are compiled to regular static methods, with a receiver object on the first parameter.
When we created our ‘toast’ extension function in the extensions.kt file, the compiler created an ExtensionsKt Java class with the static method toast(). To create a name for this class, the compiler takes the corresponding Kotlin source file (extensions), capitalizes it (Extensions), and adds ‘Kt.’ In fact, if you place your cursor inside the toast(“Button Clicked!”) line of code, and then select ‘Tools > Kotlin > Show Kotlin Bytecode’ from the Android Studio toolbar, you’ll see this static method being invoked.
You can even use this extension function in a Java class by importing it at the call site:
Member Extension Functions
We’ve been declaring extension functions directly under a package as top-level functions, but it’s also possible to define an extension function inside the class or object where you’re going to use this extension as a member extension function.
When you’re only planning to use a function in a single location, it may make more sense to define your extension as a member extension function, rather than extracting it to a dedicated extensions.kt file.
When you’re working with a member extension function, the receivers have different names:
If there’s ever a name conflict between the dispatch receiver and the extension receiver, then the compiler willalwayschoose the extension receiver.
Extension properties
If there’s one or more properties you feel are missing from a class, then you can add them by creating an extension property for that class. For example, if you regularly find yourself writing the following bit of boilerplate:
You can define the following extension property:
You can then use ‘preferences’ as though it’s a property of Context:
However, since extensions don’t insert members into a class, it’s not possible to add an extension property with a backing field, so initializers aren’t allowed for extension properties.
Before you’re able to get the value of an extension property, you’ll need to explicitly define a get() function. If you want to set the value then you’ll need to define a set() function.
Companion Object Extensions
Kotlin introduces the concept of “companion object,” which essentially replace Java’s static members. A companion object is a singleton object that belongs to the class itself, rather than an instance of the class. It contains the variables and methods that you might want to access in a static fashion.
You create a companion object by adding the ‘companion’ keyword to the object declaration inside the class. For example:
If a class has a companion object defined, then you can add a static extension function to this class, by inserting “.Companion” between the extension type and the function name:
Here, we’re defining the extension function helloWorld on the companion object myClass.Companion. Similarly to the other extension function variants we’ve looked at, you’re not actually modifying the class. Instead, you’re adding the companion object extension to the companion object.
Once you’ve defined a companion object extension, you can call the extension function as though it’s a regular static function defined inside the ‘myClass’ companion object:
Note that you’re calling this extension using class type, not class instance.
The drawback is that you can only add static extension functions to a Java or Kotlin class with the help of a companion object. This means you can only create these kinds of extensions in classes where a companion object is already explicitly defined. Although there is a open Kotlin feature request to make it possible todeclare statically accessible members for Java classes.
Potential drawbacks
Extension functions can make your code more concise, readable, and less prone to errors. Like any feature, if used incorrectly, extension functions can have the opposite effect and introduce complexities and errors into your projects.
In this final section we’re going to look at the most common pitfalls of working with extension functions and what you can do to avoid them.
Lay some ground rules
Despite how awkward and verbose some Java classes may feel when used in Android development, vanilla Java is understood by all Java developers. When you introduce custom extension functions into your code, it becomes more difficult for others to understand.
Confusing extension functions can be a particular problem when collaborating on a project with other developers, but even if you’re working on a project solo it’s still possible to get into a tangle with extension functions— especially if you get carried away and create a ton of them.
To ensure extension functions don’t end up adding complexity to your code, it’s important to stick to the following best practices:
Never use the same signature as a member function
Extension functions cannot override functions that are already defined in a class. If you define a function that has the same receiver type and the same name as one that’s already present in the receiver class, the compiler will ignore your extension function.
Your code will still compile, which means this could derail your project as every call to your extension function will execute the member function instead. Be careful not to define any extension functions that have the same signature as a member function.
Wrapping up
Kotlin’s extension functions open up lots of possibilities for adding “missing” functionality to classes. Are there any classes that you always felt were missing some important functionality? Do you plan on using extension functions to add these features? Let us know in the comments below!
Thank you for being part of our community. Read ourComment Policybefore posting.