Table of Contents
Introduction
Interfaces in Kotlin let you describe what a class can do without saying how it does it. They define a contract made of methods and properties that other classes agree to implement. This is very useful in Android projects where you want different classes to share behavior, but you do not want to force a strict class hierarchy the way inheritance does.
In this chapter you will see how to declare and use interfaces in Kotlin, how they differ from classes and abstract classes, and how they help you write flexible and testable Android code.
Declaring an Interface
An interface is declared with the interface keyword. Inside it you put method signatures and property declarations that implementing classes must provide.
interface Clickable {
fun onClick()
}
Here Clickable says that anything that implements it must have an onClick function. There is no method body yet, only the function shape.
You can also declare properties inside an interface.
interface Identifiable {
val id: String
fun describe(): String
}
The id property is abstract, which means any implementing class must provide it.
Every non‑default function and every abstract property declared in an interface must be implemented by any non‑abstract class that implements that interface.
Implementing an Interface
A class declares that it implements an interface using a colon and the interface name. Then it must provide implementations for all abstract members from that interface.
class Button(override val id: String) : Identifiable, Clickable {
override fun onClick() {
println("Button $id clicked")
}
override fun describe(): String {
return "Button with id = $id"
}
}
The override keyword is required whenever you provide an implementation of a member defined in an interface. Kotlin enforces this to make overriding explicit and clear.
If a class does not implement all interface members, it must itself be marked as abstract, which means it cannot be instantiated directly.
abstract class BaseClickable : Clickable {
// onClick not implemented here, so this class is abstract
}Interface Properties
Kotlin interfaces can declare both abstract properties and properties with default implementations using accessors.
An abstract property in an interface looks like this.
interface Named {
val name: String
}
Any class that implements Named must provide name.
class User(override val name: String) : NamedYou can also give a property a default implementation in the interface using a custom getter.
interface Titled {
val title: String
get() = "Untitled"
}
A class that implements Titled does not need to override title unless it wants a different value.
class Document : Titled
class Article(override val title: String) : Titled
In this example Document().title uses the default getter from Titled, and Article("News").title uses the value passed in the constructor.
Default Method Implementations
Unlike some older languages, Kotlin interfaces can contain concrete method implementations. This is very useful because it lets you share behavior without creating a full base class.
interface Clickable {
fun onClick()
fun showClickFeedback() {
println("Item clicked")
}
}
A class that implements Clickable must implement onClick, but it automatically gets showClickFeedback unless it chooses to override it.
class ImageButton : Clickable {
override fun onClick() {
println("ImageButton was clicked")
showClickFeedback()
}
override fun showClickFeedback() {
println("ImageButton feedback")
}
}
Here ImageButton overrides showClickFeedback, so the default implementation from the interface is not used.
Multiple Interfaces
One of the most important reasons to use interfaces is that a class can implement several of them at the same time. This gives you a flexible way to combine behaviors, something you cannot do with multiple class inheritance.