Multiple Interfaces
In this page:
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
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())
}
Login to try C/C++/Java/PHP code in the editor
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
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())
}
Login to try C/C++/Java/PHP code in the editor
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
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())
}
Login to try C/C++/Java/PHP code in the editor
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
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())
}
}
Login to try C/C++/Java/PHP code in the editor
- Assuming multiple interface implementation is the same as multiple class inheritance; Kotlin still only allows one base class but many interfaces.
- Forgetting to disambiguate when two interfaces provide the same default method name, which requires an explicit
super<InterfaceName>.method()call. - 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).
- 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: