Protocol inheritance in Swift - Time & Space Complexity
When using protocol inheritance in Swift, it's helpful to understand how the program's work grows as more protocols and conformances are involved.
We want to see how the time to check or use inherited protocols changes as the number of protocols grows.
Analyze the time complexity of the following code snippet.
protocol A {
func doA()
}
protocol B: A {
func doB()
}
protocol C: B {
func doC()
}
struct MyStruct: C {
func doA() { print("A") }
func doB() { print("B") }
func doC() { print("C") }
}
This code shows protocols inheriting from other protocols and a struct conforming to the most derived one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking protocol inheritance chain when accessing protocol requirements.
- How many times: Once per level of inheritance in the protocol chain.
Each time you add a new protocol that inherits from another, the program checks one more level when confirming requirements.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 (one protocol) | 1 check |
| 5 (five protocols in chain) | 5 checks |
| 10 (ten protocols in chain) | 10 checks |
Pattern observation: The number of checks grows directly with the number of protocols in the inheritance chain.
Time Complexity: O(n)
This means the time to verify or use inherited protocols grows linearly with the number of protocols in the inheritance chain.
[X] Wrong: "Protocol inheritance checks happen instantly no matter how many protocols there are."
[OK] Correct: Each inherited protocol adds a step to check requirements, so more protocols mean more work.
Understanding how protocol inheritance affects performance helps you write clear and efficient Swift code, a skill valuable in many coding situations.
"What if a protocol inherits from multiple protocols instead of just one? How would the time complexity change?"
Practice
Solution
Step 1: Understand protocol inheritance concept
Protocol inheritance lets you build new protocols by combining existing ones, so the new protocol requires all combined features.Step 2: Compare options with concept
Only Create a new protocol that includes requirements from one or more existing protocols correctly describes creating a new protocol that inherits requirements from existing protocols.Final Answer:
Create a new protocol that includes requirements from one or more existing protocols -> Option DQuick Check:
Protocol inheritance = create combined protocols [OK]
- Confusing protocol inheritance with class inheritance
- Thinking protocols can be instantiated
- Believing protocols can override methods
AdvancedProtocol that inherits from BasicProtocol in Swift?Solution
Step 1: Recall Swift protocol inheritance syntax
In Swift, a protocol inherits from another by listing the parent protocol after a colon (:).Step 2: Match syntax with options
Only protocol AdvancedProtocol: BasicProtocol {} uses the correct syntax:protocol NewProtocol: ExistingProtocol {}.Final Answer:
protocol AdvancedProtocol: BasicProtocol {} -> Option AQuick Check:
Protocol inheritance syntax uses colon (:) [OK]
- Using 'inherits' or 'extends' keywords which are invalid in Swift
- Using 'implements' keyword which is for classes, not protocols
- Missing the colon between protocol names
example.printInfo() is called?protocol A {
func printInfo()
}
protocol B: A {
func printExtra()
}
class Example: B {
func printInfo() {
print("Info from A")
}
func printExtra() {
print("Extra from B")
}
}
let example = Example()
example.printInfo()Solution
Step 1: Understand protocol inheritance and class conformance
Protocol B inherits from A, so class Example must implement bothprintInfo()andprintExtra(). It does.Step 2: Identify which method is called
The code callsexample.printInfo(), which prints "Info from A".Final Answer:
Info from A -> Option AQuick Check:
Calling printInfo() prints "Info from A" [OK]
- Confusing which method is called
- Expecting both methods to print automatically
- Thinking protocol inheritance affects method output
protocol P1 {
func method1()
}
protocol P2: P1 {
func method2()
}
class MyClass: P2 {
func method2() {
print("Method 2")
}
}Solution
Step 1: Analyze protocol inheritance and class conformance
Protocol P2 inherits from P1, so any class conforming to P2 must implement all methods from P1 and P2.Step 2: Check implemented methods in MyClass
MyClass implements onlymethod2()but missesmethod1(), causing a compile error.Final Answer:
MyClass must implement method1() because P2 inherits from P1 -> Option CQuick Check:
Inherited protocol methods must be implemented [OK]
- Ignoring inherited protocol requirements
- Thinking only new protocol methods must be implemented
- Confusing class and protocol roles
protocol Vehicle {
func start()
}
protocol ElectricVehicle: Vehicle {
func charge()
}You want to create a class
ElectricCar that conforms to ElectricVehicle. Which implementation correctly satisfies all protocol requirements?Solution
Step 1: Understand protocol inheritance requirements
ElectricVehicle inherits from Vehicle, so conforming types must implement bothstart()andcharge().Step 2: Check each class implementation
class ElectricCar: ElectricVehicle { func start() { print("Starting") } func charge() { print("Charging") } } implements both methods, fulfilling all requirements. Options B and D misscharge()orstart(). class ElectricCar: Vehicle { func charge() { print("Charging") } } does not conform to ElectricVehicle.Final Answer:
class ElectricCar: ElectricVehicle { func start() { print("Starting") } func charge() { print("Charging") } } -> Option BQuick Check:
Implement all inherited protocol methods in class [OK]
- Implementing only one protocol method
- Conforming to wrong protocol
- Missing methods from inherited protocols
Ready to prove it?
Pass the test and this pattern moves unknown
