Multiple interface implementation in Kotlin - Time & Space Complexity
Let's see how the time needed changes when a Kotlin class implements multiple interfaces.
We want to know how the program's work grows as we call methods from these interfaces.
Analyze the time complexity of the following code snippet.
interface A {
fun actionA()
}
interface B {
fun actionB()
}
class MultiImpl : A, B {
override fun actionA() {
println("Action A")
}
override fun actionB() {
println("Action B")
}
}
fun main() {
val obj = MultiImpl()
obj.actionA()
obj.actionB()
}
This code shows a class implementing two interfaces and calling their methods.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling each interface method once.
- How many times: Each method is called exactly one time.
Since each method runs once, the work does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 (calls to actionA and actionB) |
| 100 | 2 (still just two calls) |
| 1000 | 2 (no increase in calls) |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time needed stays constant regardless of input size.
[X] Wrong: "Implementing multiple interfaces makes the program slower as input grows."
[OK] Correct: The number of method calls does not depend on input size here, so speed stays steady.
Understanding how multiple interfaces affect performance helps you explain design choices clearly and confidently.
"What if each interface method contained a loop over a list of size n? How would the time complexity change?"