Introduction
An interface in Kotlin defines a contract of functions and properties that implementing classes must fulfill. Unlike abstract classes, interfaces cannot store state in backing fields, but they can provide default method implementations, making them a flexible tool for sharing behavior across otherwise unrelated classes.
Cricket analogy: Like an ICC playing-conditions rulebook that mandates what every match must include (overs, DRS reviews) without dictating a specific team's lineup, a Kotlin interface defines a contract of functions and properties without storing state itself.
Syntax
interface Drawable {
val color: String
fun draw()
fun describe() {
println("A $color shape")
}
}Explanation
Drawable declares an abstract property color (no backing field, so implementing classes must provide it), an abstract function draw() with no body, and a function describe() that has a default implementation. A class implements an interface using the colon : syntax and must override any abstract members with the override keyword; it may optionally override members that already have default implementations. Because interfaces have no constructors and cannot hold state directly, a class can implement multiple interfaces simultaneously without the diamond-inheritance issues that plague multiple class inheritance.
Cricket analogy: Like a Drawable-style playing-conditions contract requiring every team to declare its color (jersey scheme, no default kit provided) and follow a mandatory draw()-like coin-toss procedure, while describe() comes with a standard default script; a team can follow multiple such rulebooks without conflicting mandates like diamond inheritance would cause.
Example
interface Drawable {
val color: String
fun draw()
fun describe() {
println("A $color shape")
}
}
class Circle(override val color: String) : Drawable {
override fun draw() {
println("Drawing a $color circle")
}
}
fun main() {
val circle = Circle("red")
circle.draw()
circle.describe()
}Output
Drawing a red circle
A red shapeKey Takeaways
- Interfaces declare abstract methods and can also provide default implementations.
- Interface properties cannot have backing fields; implementers must provide the value.
- A class implements an interface with the colon syntax and overrides abstract members.
- A class can implement multiple interfaces, avoiding single-inheritance limitations.
- Default methods reduce duplicated code across multiple implementing classes.
Practice what you learned
1. Can a Kotlin interface provide a default implementation for a function?
2. Can an interface property have a backing field?
3. How many interfaces can a single Kotlin class implement?
4. What keyword is used when a class provides a value for an abstract interface property?
Was this page helpful?
You May Also Like
Classes in Kotlin
Learn how Kotlin classes bundle state and behavior with concise constructor syntax and final-by-default semantics.
Inheritance in Kotlin
Learn how open classes, open members, and the override keyword enable single-class inheritance in Kotlin.
Abstract Classes in Kotlin
Abstract classes provide a partial implementation with both abstract and concrete members that subclasses complete.
Object Declarations and Companion Objects in Kotlin
Understand Kotlin's object keyword for singletons and companion objects for class-level, Java-static-like members.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics