Interfaces and delegation
Interfaces and delegation
A school needs to print labels for different kinds of objects. A interface describes requirements a conforming type must provide, such as a property or method. It lets code depend on a capability without requiring every type to inherit from one common class.
What you’ll learn
- Implement an interface capability.
- Forward behaviour through explicit delegation.
Interfaces and delegation in the example
Capability contract
A school needs to print labels for different kinds of objects.
Capability contractConforming value
An interface states properties or functions a type must provide and may supply suitable default implementations.
Conforming valueFunction uses the contract
Student supplies the label property.
Function uses the contract
Understand interfaces and delegation
An interface states properties or functions a type must provide and may supply suitable default implementations. A class can implement several interfaces. The by delegation syntax forwards an interface implementation to another object, making composition an alternative to rebuilding every operation.
Follow interfaces and delegation
Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.
Kotlin/JVM · complete program
interface Labelled { val label: String }
data class Student(override val label: String) : Labelled
class Badge(private val owner: Labelled) : Labelled by owner
fun display(item: Labelled) { println(item.label) }
fun main() { display(Badge(Student("Diya"))) }Expected output
DiyaFollow the execution
Student supplies the label property. Badge delegates that capability to its owner, so display reads Diya through the interface. No shared superclass between student data and badge presentation is required.
Try this in the Kotlin compiler by pasting the complete program.
Practice: Interfaces and delegation
Your task: Could a Room provide the same readable label capability?
Kotlin/JVM · complete program
interface Labelled { val label: String }
data class Student(override val label: String) : Labelled
class Badge(private val owner: Labelled) : Labelled by owner
fun display(item: Labelled) { println(item.label) }
fun main() { display(Badge(Student("Diya"))) }Worked answer
Yes, by implementing Labelled and satisfying its property requirement. Display depends on the interface rather than one concrete record type.
Interfaces and delegation: the important boundary
Kotlin interfaces do not store ordinary backing fields for their properties. Resolve conflicting defaults deliberately. Delegation forwards behaviour; it does not deep-copy its delegate or automatically enforce thread safety.
Check your understanding
Interfaces and delegation: what to keep in mind
Yes, by implementing Labelled and satisfying its property requirement. Display depends on the interface rather than one concrete record type. Kotlin interfaces do not store ordinary backing fields for their properties.
Further reading: official Kotlin documentation for this topic.
Reaching the recap and completing the quizzes are separate milestones.