0
0
Kotlinprogramming~5 mins

Multiple interface implementation in Kotlin - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Multiple interface implementation
O(1)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

Since each method runs once, the work does not grow with input size.

Input Size (n)Approx. Operations
102 (calls to actionA and actionB)
1002 (still just two calls)
10002 (no increase in calls)

Pattern observation: The number of operations stays the same no matter how big the input is.

Final Time Complexity

Time Complexity: O(1)

This means the time needed stays constant regardless of input size.

Common Mistake

[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.

Interview Connect

Understanding how multiple interfaces affect performance helps you explain design choices clearly and confidently.

Self-Check

"What if each interface method contained a loop over a list of size n? How would the time complexity change?"