0
0
Kotlinprogramming~15 mins

Interface with default implementations in Kotlin - Deep Dive

Choose your learning style9 modes available
Overview - Interface with default implementations
What is it?
An interface in Kotlin is a way to define a contract that classes can follow. It can declare functions without bodies, but it can also provide default implementations for some functions. This means that classes implementing the interface can use the default behavior or override it with their own. This feature helps reduce repeated code and makes interfaces more powerful.
Why it matters
Without default implementations, every class that implements an interface must provide its own version of all functions, even if many classes share the same behavior. This leads to duplicated code and harder maintenance. Default implementations let developers write common code once in the interface, making programs cleaner and easier to update. It also allows interfaces to evolve by adding new functions without breaking existing classes.
Where it fits
Before learning this, you should understand basic Kotlin interfaces and classes. After this, you can explore advanced Kotlin features like delegation, abstract classes, and sealed classes to see different ways of sharing and controlling behavior.
Mental Model
Core Idea
An interface with default implementations is like a contract that not only says what to do but also shows how to do it unless you want to do it differently.
Think of it like...
Imagine a recipe book that gives you a standard recipe for a dish. You can follow the recipe exactly, or you can change some steps to suit your taste. The recipe book is the interface, and the default recipe is the default implementation.
┌─────────────────────────────┐
│        Interface            │
│ ┌─────────────────────────┐ │
│ │ Function A()            │ │
│ │ - default implementation│ │
│ └─────────────────────────┘ │
│ ┌─────────────────────────┐ │
│ │ Function B()            │ │
│ │ - no implementation     │ │
│ └─────────────────────────┘ │
└─────────────┬───────────────┘
              │ implements
              ▼
┌─────────────────────────────┐
│       Implementing Class    │
│ - can use default Function A│
│ - must provide Function B    │
└─────────────────────────────┘
Build-Up - 7 Steps
1
FoundationBasic Kotlin Interface Syntax
🤔
Concept: Learn how to declare an interface and functions without bodies.
interface Vehicle { fun drive() fun stop() }
Result
Defines a contract with two functions that any class implementing Vehicle must provide.
Understanding the basic interface syntax is essential because it sets the foundation for how Kotlin enforces contracts between code parts.
2
FoundationImplementing an Interface in a Class
🤔
Concept: See how a class implements an interface by providing function bodies.
class Car : Vehicle { override fun drive() { println("Car is driving") } override fun stop() { println("Car stopped") } }
Result
Car class now follows the Vehicle contract by defining drive and stop behaviors.
Knowing how classes implement interfaces helps you understand the role of interfaces as blueprints.
3
IntermediateAdding Default Implementations in Interface
🤔
Concept: Introduce default function bodies inside interfaces.
interface Vehicle { fun drive() { println("Driving") } fun stop() }
Result
drive() has a default behavior; stop() still requires implementation.
Default implementations reduce the need for every class to write the same code, improving code reuse.
4
IntermediateUsing Default Implementations in Classes
🤔
Concept: Show how classes can use or override default interface functions.
class Bike : Vehicle { override fun stop() { println("Bike stopped") } } fun main() { val bike = Bike() bike.drive() // uses default bike.stop() // uses override }
Result
Output: Driving Bike stopped
Classes can choose to accept default behavior or provide their own, giving flexibility.
5
IntermediateMultiple Interfaces with Default Methods
🤔Before reading on: If two interfaces have the same default method, do you think Kotlin picks one automatically or requires you to resolve it? Commit to your answer.
Concept: Explore how Kotlin handles conflicts when multiple interfaces provide the same default function.
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") } }