Interfaces let you define a contract for classes. Default implementations let you add common behavior so classes don't have to write the same code again.
Interface with default implementations in Kotlin
interface InterfaceName { fun functionName() { // default implementation } }
Functions inside interfaces can have a body, which is the default implementation.
Classes that implement the interface can use the default or provide their own version.
interface Greeter { fun greet() { println("Hello from default!") } }
class Person : Greeter { // Uses default greet() }
class Robot : Greeter { override fun greet() { println("Beep boop, hello!") } }
This program shows an interface Speaker with a default speak function. Human uses the default speak, while Parrot overrides it.
interface Speaker { fun speak() { println("Speaking in a normal voice.") } } class Human : Speaker { // Uses default speak() } class Parrot : Speaker { override fun speak() { println("Squawk! Hello!") } } fun main() { val person = Human() val bird = Parrot() person.speak() // Calls default implementation bird.speak() // Calls overridden implementation }
Default implementations help avoid repeating code in every class.
If a class overrides a function, its version is used instead of the default.
Interfaces can have multiple functions with or without default implementations.
Interfaces define what functions a class should have.
Default implementations provide a basic function body inside interfaces.
Classes can use or override these default functions as needed.
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
