Why inheritance is class-only in Swift - Performance Analysis
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.
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.
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.
Method dispatch time does not grow with inheritance depth thanks to vtables.
| Inheritance Depth (n) | Approx. Operations |
|---|---|
| 1 | 1 vtable lookup |
| 5 | 1 vtable lookup |
| 10 | 1 vtable lookup |
Pattern observation: The cost is constant (O(1)) regardless of the depth of the inheritance chain.
Time Complexity: O(1)
This means the time to dispatch a method is constant, even as the inheritance chain gets longer.
[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.
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.
"Why can't structs inherit? How would adding dynamic dispatch to structs impact performance compared to classes?"
Practice
Solution
Step 1: Understand type behavior in Swift
Classes are reference types, meaning instances share the same data in memory.Step 2: Compare with structs and enums
Structs and enums are value types, so they copy data instead of sharing it.Final Answer:
Because classes are reference types and share data by reference -> Option CQuick Check:
Inheritance = Reference types only [OK]
- Thinking structs can inherit like classes
- Believing enums can have inheritance
- Confusing methods with inheritance
Solution
Step 1: Identify inheritance syntax in Swift
Only classes can inherit from other classes using the colon syntax.Step 2: Check each declaration
Structs, enums, and protocols cannot inherit from classes.Final Answer:
class MyClass: ParentClass {} -> Option AQuick Check:
Inheritance syntax = class only [OK]
- Trying to inherit a class in a struct
- Using enum to inherit a class
- Confusing protocol inheritance with class inheritance
struct ParentStruct {}
struct ChildStruct: ParentStruct {}Solution
Step 1: Analyze struct inheritance rules
Structs in Swift cannot inherit from other structs or classes.Step 2: Predict compiler behavior
The compiler will produce an error because inheritance is not allowed for structs.Final Answer:
Compile-time error: structs cannot inherit -> Option BQuick Check:
Struct inheritance = error [OK]
- Assuming structs inherit like classes
- Expecting runtime error instead of compile error
- Thinking structs copy properties via inheritance
enum ParentEnum {}
class ChildClass: ParentEnum {}Solution
Step 1: Check inheritance rules for enums and classes
Enums are value types and cannot be inherited or used as base classes.Step 2: Understand class inheritance restrictions
Classes can only inherit from other classes, not enums.Step 3: Combine observations
All statements about enums and classes here are correct.Final Answer:
All of the above -> Option DQuick Check:
Enum inheritance = disallowed [OK]
- Thinking enums can be base classes
- Confusing enum cases with inheritance
- Ignoring that classes can't inherit enums
Solution
Step 1: Recall inheritance limits
Inheritance works only for classes, so structs cannot inherit from classes.Step 2: Identify alternative sharing method
Protocols allow both structs and classes to share behavior by conforming to them.Step 3: Evaluate options
Using protocols is the best way to share behavior without inheritance.Final Answer:
Use a protocol to define shared behavior -> Option AQuick Check:
Protocols share behavior across types [OK]
- Trying to inherit struct from class
- Converting structs unnecessarily to classes
- Thinking enums can inherit
Ready to prove it?
Pass the test and this pattern moves unknown
