100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Extension Functions in Kotlin

Learn how Kotlin extension functions let you add new behavior to existing classes without modifying or inheriting them.

Functions & LambdasIntermediate8 min readJul 8, 2026
Analogies

Introduction

Extension functions let you add new functions to existing classes, including classes you don't own or can't modify, such as those from the standard library or third-party code. Instead of inheriting from a class or using a wrapper, you define a function outside the class that appears to be a member of it. This is a purely syntactic feature: the class itself is never modified, and no new members are actually inserted into it.

🏏

Cricket analogy: Commentators unofficially label a bowler's signature delivery the doosra without the ICC rulebook itself being rewritten; an extension function similarly lets you call String.lastChar() without ever touching Kotlin's own String class source.

Syntax

kotlin
fun String.lastChar(): Char = this[this.length - 1]

// Called just like a member function
val c = "hello".lastChar()

Explanation

The type before the dot (String) is called the receiver type — the type being extended. Inside the function body, this refers to the receiver instance, in this case the string the function was called on. Once declared, lastChar() can be called on any String value exactly as if it were a real member function. An important gotcha is that extension functions are resolved statically at compile time based on the declared type of the expression, not the actual runtime type — this means they do NOT participate in true polymorphism or virtual dispatch the way member function overrides do.

🏏

Cricket analogy: Once a scorer decides a batsman is the striker (the receiver), all further ball-by-ball notes use this batsman to mean him specifically, but the decision is locked in before the ball is bowled, not adjusted mid-delivery, just as extension functions resolve to the declared type at compile time.

Example

kotlin
fun String.lastChar(): Char = this[this.length - 1]

fun Int.isEven(): Boolean = this % 2 == 0

open class Animal
class Dog : Animal()

fun Animal.describe() = "an Animal"
fun Dog.describe() = "a Dog"

fun printDescription(animal: Animal) {
    println(animal.describe())
}

fun main() {
    println("hello".lastChar())
    println(4.isEven())
    printDescription(Dog())
}

Output

kotlin
o
true
an Animal

Key Takeaways

  • Extension functions are declared as fun ReceiverType.functionName(): ReturnType.
  • Inside the function, this refers to the receiver instance.
  • They let you add functions to classes without modifying source code or using inheritance.
  • Extension functions are resolved statically, based on the declared (compile-time) type, not the runtime type.
  • Because of static resolution, extension functions do NOT support true polymorphic/virtual dispatch.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#ExtensionFunctionsInKotlin#Extension#Functions#Syntax#Explanation#StudyNotes#SkillVeris