Kotlin 2 · Classes and types

Interfaces and delegation

Implement an interface by delegating to another object with by.

interface Logger { fun log(message: String) }
class ConsoleLogger : Logger { override fun log(message: String) = println(message) }
class Service(logger: Logger) : Logger by logger

Official Kotlin 2 documentation

More classes and types commands

All classes and types commands · Kotlin 2 cheatsheet

Was this page helpful?