interface
The interface keyword in Kotlin defines a contract of methods and properties that a class must implement. Like Java, Kotlin interfaces support default implementations. A class can implement multiple interfaces, allowing flexible design without the pitfalls of multiple inheritance.
Syntax
// Define an interface
interface Drawable {
fun draw() // Abstract method (must be overridden)
fun clear() = println("Clearing") // Default implementation
val color: String // Abstract property
}
// Implement the interface
class Circle(override val color: String) : Drawable {
override fun draw() = println("Drawing a $color circle")
}
// Implement multiple interfaces
class Button : Drawable, Clickable {
override val color = "Blue"
override fun draw() = println("Drawing a button")
override fun onClick() = println("Button clicked")
}
Syntax overview
| Syntax | Description |
|---|---|
| interface Name { } | Defines an interface. |
| fun methodName() | Declares an abstract method (implementing classes must override it). |
| fun methodName() = expression | Declares a method with a default implementation (can be overridden). |
| val propertyName: Type | Declares an abstract property (implementing classes must override it). |
| ClassName : InterfaceName | Implements an interface. |
| super<InterfaceName>.method() | Explicitly calls a default implementation from a specific interface. |
Sample code
// Define interfaces
interface Animal {
val name: String
fun sound(): String
fun introduce() = println("I am ${name}. I say ${sound()}.")
}
interface Pet {
fun play() = println("Playing!")
fun groom() = println("Grooming...")
}
// Implement multiple interfaces
class Dog(override val name: String) : Animal, Pet {
override fun sound() = "Woof"
override fun play() {
super.play() // Call the default implementation
println(" Fetching the ball!")
}
}
class Cat(override val name: String) : Animal, Pet {
override fun sound() = "Meow"
}
// Resolving conflicting default implementations
interface A {
fun hello() = println("Hello from A")
}
interface B {
fun hello() = println("Hello from B")
}
class C : A, B {
// When both interfaces have a default implementation, you must explicitly choose one.
override fun hello() {
super<A>.hello()
super<B>.hello()
println("Additional logic in C")
}
}
fun main() {
val dog = Dog("Rex")
dog.introduce() // Animal default implementation
dog.play() // Overridden method
dog.groom() // Pet default implementation
println()
val cat = Cat("Whiskers")
cat.introduce()
println()
// Treat objects as their interface type
val animals: List<Animal> = listOf(Dog("Buddy"), Cat("Luna"))
animals.forEach { it.introduce() }
println()
// Resolve conflicting default implementations
C().hello()
}
Notes
Kotlin interfaces support default implementations, giving them similar capabilities to Java 8 interfaces. However, interfaces cannot hold state (field values) — properties must either be computed via a getter or delegated to the implementing class.
When two interfaces share a default implementation with the same name, the implementing class must explicitly select one using super<InterfaceName>.method().
For the difference between interfaces and inheritance, see open / override. For the singleton pattern, see object / companion object.
If you find any errors or copyright issues, please contact us.