Sobes.tech
Junior — Middle

Is it possible to implement multiple inheritance in Kotlin?

sobes.tech AI

Answer from AI

In Kotlin, multiple inheritance of classes is not directly supported, similar to Java. A class can only inherit from one class.

However, Kotlin supports multiple inheritance of interfaces, which can contain default implementations. This allows implementing behavior similar to multiple inheritance.

Example:

interface A {
    fun foo() { println("A") }
}

interface B {
    fun foo() { println("B") }
}

class C : A, B {
    override fun foo() {
        super<A>.foo()
        super<B>.foo()
    }
}

fun main() {
    val c = C()
    c.foo() // Will output:
             // A
             // B
}

Thus, multiple inheritance of classes in Kotlin is not possible, but multiple inheritance of interfaces with implementations can be achieved.