Delegation Patterns in Kotlin
From the Kotlin cheat sheet · Delegation · verified Jul 2026
Delegation Patterns
Class delegation and delegated properties
kotlin
// Class delegation
interface Base {
fun print()
val message: String
}
class BaseImpl(val x: Int) : Base {
override fun print() = println(x)
override val message = "BaseImpl: $x"
}
class Derived(b: Base) : Base by b {
// Can override members
override val message = "Derived message"
}
val base = BaseImpl(10)
val derived = Derived(base)
derived.print() // Delegates to base
// Delegated properties
class User {
// Lazy property
val lazyValue: String by lazy {
println("Computing lazy value")
"Hello"
}
// Observable property
var name: String by Delegates.observable("Initial") {
prop, old, new ->
println("$old -> $new")
}
// Vetoable property
var age: Int by Delegates.vetoable(0) {
prop, old, new ->
new >= 0 // Reject negative values
}
}💡 by keyword enables delegation for classes and properties
⚡ lazy delegates compute value only once on first access
📌 observable and vetoable track/validate property changes
🟢 Map delegation useful for dynamic property storage