0
0
Kotlinprogramming~15 mins

Interface declaration and implementation in Kotlin - Deep Dive

Choose your learning style9 modes available
Overview - Interface declaration and implementation
What is it?
An interface in Kotlin is like a contract that defines a set of functions without giving their full details. Classes can then promise to follow this contract by implementing the interface, providing the actual function details. This helps different classes share common behavior while keeping their own unique ways of doing things. Interfaces can also have properties and default function implementations.
Why it matters
Interfaces exist to help organize code so different parts can work together smoothly without knowing all the details. Without interfaces, programs would be messy and hard to change because everything would be tightly connected. Interfaces let programmers write flexible and reusable code, making it easier to build and maintain large projects.
Where it fits
Before learning interfaces, you should understand basic Kotlin classes and functions. After mastering interfaces, you can explore advanced topics like abstract classes, delegation, and design patterns that rely on interfaces.
Mental Model
Core Idea
An interface is a promise that a class will provide certain functions, allowing different classes to be used interchangeably based on shared behavior.
Think of it like...
Think of an interface like a job description listing tasks someone must do, but not how to do them. Different people (classes) can have the same job description but perform tasks in their own style.
┌───────────────┐
│   Interface   │
│───────────────│
│ + funA()      │
│ + funB()      │
└─────┬─────────┘
      │ implements
┌─────▼─────────┐      ┌─────▼─────────┐
│ Class1        │      │ Class2        │
│───────────────│      │───────────────│
│ funA() { ...} │      │ funA() { ...} │
│ funB() { ...} │      │ funB() { ...} │
└───────────────┘      └───────────────┘
Build-Up - 7 Steps
1
FoundationWhat is an Interface in Kotlin
🤔
Concept: Introduce the basic idea of an interface as a contract with function declarations but no implementations.
In Kotlin, an interface is declared using the 'interface' keyword. It can contain abstract functions (without body) and properties. For example: interface Drivable { fun drive() val maxSpeed: Int } This means any class implementing Drivable must provide 'drive' function and 'maxSpeed' property.
Result
You understand that interfaces define what functions and properties a class must have, but not how they work.
Understanding that interfaces separate 'what' from 'how' helps you design flexible code where different classes can share the same capabilities.
2
FoundationImplementing an Interface in a Class
🤔
Concept: Show how a class promises to follow an interface by implementing its functions and properties.
To implement an interface, a class uses a colon and the interface name. It must provide all functions and properties declared in the interface. Example: class Car : Drivable { override val maxSpeed = 200 override fun drive() { println("Car is driving at speed $maxSpeed") } } Here, Car provides the details for 'drive' and 'maxSpeed'.
Result
The class Car now fulfills the Drivable contract and can be used wherever Drivable is expected.
Knowing how to implement interfaces lets you create classes that fit into shared roles, enabling code reuse and polymorphism.
3
IntermediateInterfaces with Default Implementations
🤔Before reading on: do you think interfaces can provide function bodies in Kotlin? Commit to yes or no.
Concept: Kotlin interfaces can include default function implementations, so classes can use or override them.
Unlike some languages, Kotlin allows interfaces to have functions with default bodies: interface Drivable { fun drive() { println("Driving") } } class Bike : Drivable { // Uses default drive() } class Truck : Drivable { override fun drive() { println("Truck driving") } } This means classes can skip implementing functions if the default works.
Result
Classes can share common behavior through interfaces, reducing repeated code.
Understanding default implementations in interfaces helps you write cleaner code and avoid unnecessary duplication.
4
IntermediateProperties in Interfaces
🤔Before reading on: can interfaces in Kotlin have properties with backing fields? Commit to yes or no.
Concept: Interfaces can declare properties but cannot store data; they can have abstract or default getters/setters.
In Kotlin, interfaces can declare properties like this: interface Identifiable { val id: String } Or with default getter: interface Named { val name: String get() = "Unknown" } Classes implementing these must provide the property or use the default getter. Interfaces cannot hold actual data fields.
Result
You learn that interfaces describe properties but do not store values themselves.
Knowing that interfaces only describe properties but don't hold data clarifies how Kotlin separates behavior from state.
5
IntermediateMultiple Interfaces and Conflict Resolution
🤔Before reading on: if two interfaces have the same default function, which one does a class use? Commit to your guess.
Concept: Kotlin allows a class to implement multiple interfaces, and if conflicts arise, the class must resolve them explicitly.
Example: interface A { fun greet() { println("Hello from A") } } interface B { fun greet() { println("Hello from B") } } class C : A, B { override fun greet() { super.greet() super.greet() println("Hello from C") } } Here, class C must override greet() and specify which interface's greet() to call.