Interface with default implementations in Kotlin - Time & Space Complexity
We want to understand how the time needed to run code changes when using interfaces with default implementations in Kotlin.
Specifically, how does calling default methods affect the work done as input grows?
Analyze the time complexity of the following code snippet.
interface Printer {
fun printAll(items: List) {
for (item in items) {
print(item)
}
}
}
class ConsolePrinter : Printer
fun main() {
val printer = ConsolePrinter()
val data = List(5) { "Hello" }
printer.printAll(data)
}
This code defines an interface with a default method that prints all items in a list, then uses it in a class.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside the default method
printAllthat goes through each item in the list. - How many times: It runs once for every item in the input list.
As the list size grows, the number of print operations grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The work grows directly with the number of items; doubling items doubles the work.
Time Complexity: O(n)
This means the time to run the method grows in a straight line with the number of items to print.
[X] Wrong: "Using default methods in interfaces makes the code run faster or slower by itself."
[OK] Correct: The default method is just code that runs like any other function; its speed depends on what it does, not where it is defined.
Understanding how default interface methods work helps you explain code design choices and their impact on performance clearly and confidently.
"What if the default method called another method that itself loops over the list? How would the time complexity change?"
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
