0
0
Swiftprogramming~5 mins

Why inheritance is class-only in Swift - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why inheritance is class-only in Swift
O(1)
Understanding Time Complexity

We want to understand how the cost of using inheritance in Swift grows as programs get bigger.

Specifically, why inheritance works only with classes (not structs) and what that means for performance: constant-time dynamic dispatch.

Scenario Under Consideration

Analyze the time complexity of method calls in a class inheritance chain.


class Animal {
    func speak() {
        print("Animal sound")
    }
}

class Dog: Animal {
    override func speak() {
        print("Bark")
    }
}

let pet = Dog()
pet.speak()
    

This code shows a simple class inheritance where Dog inherits from Animal and overrides a method. Swift uses dynamic dispatch via vtables.

Identify Repeating Operations

Look at what happens when methods are called through inheritance.

  • Primary operation: Dynamic method dispatch using the class's virtual method table (vtable).
  • How many times: Once per method call; vtable lookup is constant time, independent of hierarchy depth.
How Execution Grows With Input

Method dispatch time does not grow with inheritance depth thanks to vtables.

Inheritance Depth (n)Approx. Operations
11 vtable lookup
51 vtable lookup
101 vtable lookup

Pattern observation: The cost is constant (O(1)) regardless of the depth of the inheritance chain.

Final Time Complexity

Time Complexity: O(1)

This means the time to dispatch a method is constant, even as the inheritance chain gets longer.

Common Mistake

[X] Wrong: "Method dispatch in class inheritance walks the hierarchy linearly, taking O(n) time."

[OK] Correct: Swift classes use vtables for O(1) dynamic dispatch. Structs lack inheritance because they are value types without reference identity or dynamic dispatch, avoiding copying issues and enabling static dispatch via protocols.

Interview Connect

Explaining O(1) dispatch and why inheritance is class-only shows deep knowledge of Swift's type system, value vs. reference semantics, and performance trade-offs.

Self-Check

"Why can't structs inherit? How would adding dynamic dispatch to structs impact performance compared to classes?"