What if you could write common code once and never repeat it again?
Why Interface with default implementations in Kotlin? - Purpose & Use Cases
Imagine you have many classes that need to share some common behavior, but each class also has its own unique parts. Without interfaces with default implementations, you would have to write the same code again and again in every class.
Writing the same code repeatedly is slow and boring. It's easy to make mistakes or forget to update all copies when you want to change something. This wastes time and causes bugs.
Interfaces with default implementations let you write shared code once inside the interface. Classes can then use this code automatically, only adding what's unique. This saves time and keeps code clean and consistent.
interface Animal {
fun sound()
}
class Dog : Animal {
override fun sound() {
println("Bark")
}
}
class Cat : Animal {
override fun sound() {
println("Meow")
}
}interface Animal {
fun sound() {
println("Some sound")
}
}
class Dog : Animal {
override fun sound() {
println("Bark")
}
}
class Cat : Animal {}You can create flexible and reusable code that is easier to maintain and extend.
Think of a music app where many instruments can play sounds. The interface can provide a default way to play a sound, but each instrument can customize it if needed.
Writing shared code once inside interfaces saves time.
Classes can use default behavior or provide their own.
This leads to cleaner, easier-to-maintain code.
Practice
What is the main purpose of providing a default implementation in a Kotlin interface?
interface Example { fun greet() { println("Hello") } }
Solution
Step 1: Understand default implementations in interfaces
Default implementations provide a function body inside the interface itself.Step 2: Effect on implementing classes
Classes can use this default function without writing their own version.Final Answer:
To allow classes to use the function without implementing it themselves -> Option AQuick Check:
Default implementation means no need to override [OK]
- Thinking default means function cannot be overridden
- Confusing default implementation with abstract function
- Believing interface becomes unusable with default methods
Which of the following is the correct syntax for a Kotlin interface with a default implementation of a function fun info()?
Solution
Step 1: Recall Kotlin interface function syntax
Default implementations use = followed by expression or block with braces.Step 2: Check each option
interface Info { fun info() = println("Info") } uses = with println(), which is correct syntax for default implementation.Final Answer:
interface Info { fun info() = println("Info") } -> Option AQuick Check:
Default function uses = expression [OK]
- Using braces without = in interface default functions
- Using arrow -> instead of =
- Writing function body outside interface
What will be the output of the following Kotlin code?
interface Printer {
fun print() { println("Printing from interface") }
}
class Document : Printer {
override fun print() { println("Printing from Document") }
}
fun main() {
val doc: Printer = Document()
doc.print()
}Solution
Step 1: Understand override behavior
The class Document overrides the print() function from Printer interface.Step 2: Check which print() is called
Even though the variable type is Printer, the actual object is Document, so overridden print() runs.Final Answer:
Printing from Document -> Option CQuick Check:
Overridden method runs on object type [OK]
- Thinking interface default method runs instead of override
- Confusing variable type with object type
- Expecting compilation error for override
Identify the error in this Kotlin code snippet:
interface Logger {
fun log() { println("Logging") }
}
class FileLogger : Logger {
fun log() { println("File logging") }
}Solution
Step 1: Check interface function declaration
Logger interface has a default implementation of log().Step 2: Check FileLogger's log() function
It defines log() but misses the override keyword, which is required.Final Answer:
Missing override keyword in FileLogger's log() function -> Option BQuick Check:
Override keyword required when overriding [OK]
- Forgetting override keyword
- Thinking interface functions cannot have bodies
- Assuming abstract keyword needed in interface
Given this Kotlin interface and classes, what will be the output when main() runs?
interface Speaker {
fun speak() { println("Speaking from Speaker") }
}
class Person : Speaker
class Robot : Speaker {
override fun speak() { println("Speaking from Robot") }
}
fun main() {
val list: List<Speaker> = listOf(Person(), Robot())
for (item in list) {
item.speak()
}
}Solution
Step 1: Analyze Person class
Person does not override speak(), so it uses Speaker's default implementation.Step 2: Analyze Robot class
Robot overrides speak(), so its own version runs.Step 3: Loop output
First item (Person) prints "Speaking from Speaker", second (Robot) prints "Speaking from Robot".Final Answer:
Speaking from Speaker Speaking from Robot -> Option DQuick Check:
Default + override methods run accordingly [OK]
- Expecting Person to print "Speaking from Person"
- Thinking missing override causes error
- Assuming both print Robot's message
Ready to prove it?
Pass the test and this pattern moves unknown
