What if you could easily mix and match features without rewriting code every time?
Why Multiple interface implementation in Kotlin? - Purpose & Use Cases
Imagine you are building a smart home system. You want a device that can both play music and control lights. Without multiple interface implementation, you would have to create separate devices or write repetitive code for each feature.
Manually combining features means copying code or creating complex inheritance trees. This leads to errors, harder maintenance, and less flexible designs. Adding new features becomes a big headache.
Multiple interface implementation lets you define separate feature contracts and combine them easily. Your device can promise to do many things by implementing multiple interfaces, keeping code clean and reusable.
class MusicPlayer { fun playMusic() {} } class LightController { fun turnOnLight() {} } class SmartDevice : MusicPlayer() { /* no light control */ }
interface MusicPlayer { fun playMusic() }
interface LightController { fun turnOnLight() }
class SmartDevice : MusicPlayer, LightController {
override fun playMusic() {}
override fun turnOnLight() {}
}You can build flexible, modular devices or classes that combine many abilities without messy code duplication.
A smartphone app that implements interfaces for calling, messaging, and GPS navigation all in one place, making it easy to add or change features.
Manual combination of features is slow and error-prone.
Multiple interface implementation allows clean, flexible code.
It helps build modular and maintainable programs.
Practice
Solution
Step 1: Understand interface implementation
Implementing interfaces means the class must provide the functions declared in those interfaces.Step 2: Check multiple interfaces
When multiple interfaces are implemented, the class must provide all functions from each interface.Final Answer:
The class agrees to provide all functions defined in all those interfaces. -> Option CQuick Check:
Multiple interface implementation = implement all interface functions [OK]
- Thinking interfaces provide code automatically
- Believing only one interface's functions are needed
- Assuming class can't have its own functions
MyClass implementing interfaces InterfaceA and InterfaceB?Solution
Step 1: Recall Kotlin interface syntax
Kotlin uses a colon (:) to implement interfaces, separated by commas.Step 2: Check each option
class MyClass : InterfaceA, InterfaceB {} uses colon and commas correctly. Options B and C use Java-like keywords. class MyClass : InterfaceA & InterfaceB {} uses & which is invalid in Kotlin.Final Answer:
class MyClass : InterfaceA, InterfaceB {} -> Option AQuick Check:
Use ':' and commas for interfaces [OK]
- Using 'implements' keyword like Java
- Using 'extends' for interfaces
- Using '&' to combine interfaces
interface A { fun greet() = println("Hello from A") }
interface B { fun greet() = println("Hello from B") }
class C : A, B {
override fun greet() {
super<A>.greet()
super<B>.greet()
}
}
fun main() {
val c = C()
c.greet()
}Solution
Step 1: Understand interface default methods
Both interfaces A and B have greet() with default implementations.Step 2: Check class C's override
C overrides greet() and calls both super<A>.greet() and super<B>.greet() in order.Step 3: Predict output
First prints "Hello from A", then "Hello from B" on separate lines.Final Answer:
Hello from A Hello from B -> Option AQuick Check:
Calling both super interface methods prints both greetings [OK]
- Assuming compilation error due to method conflict
- Only calling one interface's greet()
- Mixing order of greetings
D implements interfaces X and Y:interface X { fun foo() }
interface Y { fun bar() }
class D : X, Y {
override fun foo() { println("foo") }
}Solution
Step 1: Check implemented interfaces
D implements X and Y, so must override all their functions.Step 2: Verify overridden functions
D overrides foo() from X but does not override bar() from Y.Step 3: Identify error
Missing override of bar() causes compilation error.Final Answer:
Missing override of bar() from interface Y -> Option BQuick Check:
All interface functions must be overridden [OK]
- Forgetting to override all interface methods
- Thinking multiple interfaces are not allowed
- Assuming override is optional
interface Printer { fun print() }
interface Scanner { fun scan() }You want a class
MultiFunctionDevice that implements both and logs each action. Which is the best way to implement this?Solution
Step 1: Understand requirement
MultiFunctionDevice must implement both Printer and Scanner interfaces.Step 2: Check options
class MultiFunctionDevice : Printer, Scanner { override fun print() { println("Printing...") } override fun scan() { println("Scanning...") } } implements both interfaces and overrides both functions with logging. Options A and B implement only one interface each. class MultiFunctionDevice { fun print() { println("Printing...") } fun scan() { println("Scanning...") } } does not implement interfaces.Final Answer:
class MultiFunctionDevice : Printer, Scanner { override fun print() { println("Printing...") } override fun scan() { println("Scanning...") } } -> Option DQuick Check:
Implement all interfaces and override all methods [OK]
- Implementing only one interface when multiple are needed
- Not overriding interface methods
- Not declaring interfaces in class header
Ready to prove it?
Pass the test and this pattern moves unknown
