0Pricing
Kotlin Academy · Lesson

The by Keyword

Delegate interface implementation.

What Is Delegation?

Class delegation lets one object hand off an interface's implementation to another object. Kotlin builds this into the language with the by keyword.

It is composition made effortless.

An Interface to Delegate

Start with an interface and a concrete implementation. We will later delegate to this implementation.

interface Speaker {
    fun speak(): String
}

class LoudSpeaker : Speaker {
    override fun speak() = "HELLO!"
}

fun main() {
    println(LoudSpeaker().speak())
}

All lessons in this course

  1. The by Keyword
  2. Delegating to Members
  3. Delegation vs Inheritance
  4. Practical Patterns
← Back to Kotlin Academy