Interface with default implementations

suggest change

An interface in Kotlin can have default implementations for functions:

interface MyInterface {
    fun withImplementation() {
      print("withImplementation() was called")
    }
}

Classes implementing such interfaces will be able to use those functions without reimplementing

class MyClass: MyInterface {
    // No need to reimplement here
}
val instance = MyClass()
instance.withImplementation()

Properties

Default implementations also work for property getters and setters:

interface MyInterface2 {
    val helloWorld
        get() = "Hello World!"
}

Interface accessors implementations can’t use backing fields

interface MyInterface3 {
    // this property won't compile!
    var helloWorld: Int
        get() = field
        set(value) { field = value }
}

Multiple implementations

When multiple interfaces implement the same function, or all of them define with one or more implementing, the derived class needs to manually resolve proper call

interface A {
    fun notImplemented()
    fun implementedOnlyInA() { print("only A") }
    fun implementedInBoth() { print("both, A") }
    fun implementedInOne() { print("implemented in A") }
}

interface B {
    fun implementedInBoth() { print("both, B") }
    fun implementedInOne() // only defined
}

class MyClass: A, B {
    override fun notImplemented() { print("Normal implementation") }

    // implementedOnlyInA() can by normally used in instances

    // class needs to define how to use interface functions
    override fun implementedInBoth() {
        super<B>.implementedInBoth()
        super<A>.implementedInBoth()
    }

    // even if there's only one implementation, there multiple definitions
    override fun implementedInOne() {
        super<A>.implementedInOne()
        print("implementedInOne class implementation")
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents