← Back to Kotlin Course | Chapter 7: Inheritance & Interfaces | Lesson 5 of 7

Multiple Interfaces

Multiple interfaces let one class agree to several separate checklists of abilities all at once, something a single base class can't do.

Implementing Several Interfaces

A class lists all the interfaces it implements after the colon, separated by commas, and must satisfy every abstract member from each one.

Example: Implementing Several Interfaces

markup
interface Flyable {
    fun fly(): String
}

interface Swimmable {
    fun swim(): String
}

class Duck : Flyable, Swimmable {
    override fun fly(): String = "Duck is flying"
    override fun swim(): String = "Duck is swimming"
}

fun main() {
    val duck = Duck()
    println(duck.fly())
    println(duck.swim())
}

Resolving Conflicting Default Methods

If two interfaces both provide a default implementation for a method with the same name, the implementing class must override it itself to resolve the ambiguity.

Example: Resolving Conflicting Default Methods

markup
interface A {
    fun describe(): String = "From A"
}

interface B {
    fun describe(): String = "From B"
}

class C : A, B {
    override fun describe(): String = super<A>.describe() + " and " + super<B>.describe()
}

fun main() {
    println(C().describe())
}

Combining a Base Class and Interfaces

When a class both extends a base class and implements interfaces, the base class comes first in the list, followed by the interfaces, all separated by commas.

Example: Combining a Base Class and Interfaces

markup
open class Animal(val name: String)

interface Trainable {
    fun train(): String
}

class Dog(name: String) : Animal(name), Trainable {
    override fun train(): String = "$name learned to sit"
}

fun main() {
    val dog = Dog("Rex")
    println(dog.train())
}

Interfaces as Type Contracts

A variable can be declared with an interface type even when it holds an instance of a concrete class, letting code depend only on the shared contract rather than a specific implementation.

Example: Interfaces as Type Contracts

markup
interface Playable {
    fun play(): String
}

class Song : Playable {
    override fun play(): String = "Playing a song"
}

class Video : Playable {
    override fun play(): String = "Playing a video"
}

fun main() {
    val items: List<Playable> = listOf(Song(), Video())
    for (item in items) {
        println(item.play())
    }
}
Common Mistakes
  1. Assuming multiple interface implementation is the same as multiple class inheritance; Kotlin still only allows one base class but many interfaces.
  2. Forgetting to disambiguate when two interfaces provide the same default method name, which requires an explicit super<InterfaceName>.method() call.
  3. Listing interfaces in the wrong place, after a base class, without realizing order there doesn't matter for interfaces (though the class must come first if present).
Chapter Summary
  • A class can implement any number of interfaces, separated by commas after the colon.
  • When two implemented interfaces provide the same default method, the class must override it and can call a specific one with super<InterfaceName>.method().
  • A single base class (if any) is listed first, followed by interfaces, all separated by commas.
  • Implementing multiple interfaces is how Kotlin achieves a form of multiple inheritance for behavior contracts.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.