Skip to content

What are extension functions

Devrath edited this page Mar 6, 2024 · 1 revision

What is an Extension function

  • Extension functions are a feature in Kotlin that allows you to add new functions to existing classes without modifying their source code.
  • This means you can extend the functionality of classes, including standard library classes, third-party classes, or your custom classes, without the need for inheritance or altering the original class.

Code

fun main(args: Array<String>) {
    val result = "Panda".addCustomString()
    println(result)
}


fun String.addCustomString() : String {
    val customString = " Horray!!"
    return  this.plus(customString)
}

Output

Panda Horray!!
Clone this wiki locally