Search⌘ K
AI Features

Introduction to Extensions

Explore Kotlin extension functions to understand how you can add new functionalities to existing classes without changing their source code. Discover the role of the this keyword and how extensions differ from member functions, empowering you to write cleaner and more modular Kotlin code.

Introduction to class members

The most intuitive way to define methods and properties is inside classes. Such elements are called:

  • Class members

  • Member functions

  • Member properties

Kotlin 1.5
class Telephone(
// member property
val number: String
) {
// member function
fun call() {
print("Calling $number")
}
}
fun main() {
// Usage
val telephone = Telephone("123456789")
println(telephone.number) // 123456789
telephone.call() // Calling 123456789
}

Introduction to extension functions

Kotlin allows another way to define functions and properties that are ...