Protocol inheritance means one protocol can require all the properties and methods of another protocol. It lets you build new protocols based on existing ones, like stacking rules.
Click to reveal answer
beginner
How do you declare a protocol that inherits from another protocol in Swift?
You write the new protocol name followed by a colon and the name of the protocol it inherits from. Example: protocol B: A { } means B inherits from A.
Click to reveal answer
intermediate
Can a protocol inherit from multiple protocols in Swift?
Yes, a protocol can inherit from more than one protocol by listing them separated by commas. Example: protocol C: A, B { } means C inherits from both A and B.
Click to reveal answer
intermediate
What happens if a protocol inherits from another protocol with required properties or methods?
The inheriting protocol also requires those properties or methods. Any type adopting the new protocol must implement all requirements from both protocols.
Click to reveal answer
beginner
Why use protocol inheritance instead of just one big protocol?
Protocol inheritance helps keep code organized and flexible. You can build small, focused protocols and combine them as needed, making your code easier to understand and reuse.
Click to reveal answer
How do you declare a protocol B that inherits from protocol A in Swift?
Aprotocol B: A { }
Bprotocol B inherits A { }
Cprotocol B extends A { }
Dprotocol B -> A { }
✗ Incorrect
In Swift, protocol inheritance uses a colon ':' to specify the parent protocol.
Can a Swift protocol inherit from multiple protocols?
AYes, by listing protocols separated by commas
BYes, but only two protocols max
CNo, only one protocol can be inherited
DNo, protocols cannot inherit
✗ Incorrect
Swift allows multiple protocol inheritance by listing protocols separated by commas.
If protocol B inherits from protocol A, what must a type conforming to B do?
AImplement only B's requirements
BNo implementation needed
CImplement only A's requirements
DImplement requirements of both A and B
✗ Incorrect
A type conforming to B must satisfy all requirements from both B and A.
Why is protocol inheritance useful?
ATo avoid writing any code
BTo create one big protocol with all features
CTo organize and combine small protocols flexibly
DTo make protocols slower
✗ Incorrect
Protocol inheritance helps organize code by combining small, focused protocols.
Which keyword is used to declare a protocol in Swift?
Astruct
Bprotocol
Cclass
Dinterface
✗ Incorrect
The keyword protocol is used to declare protocols in Swift.
Explain how protocol inheritance works in Swift and why it is useful.
Think about how one protocol can build on another and how that helps keep code clean.
You got /4 concepts.
Describe what a type must do when it conforms to a protocol that inherits from other protocols.
Remember that inherited protocols add to the list of rules a type must follow.
You got /3 concepts.
Practice
(1/5)
1. What does protocol inheritance in Swift allow you to do?
easy
A. Create instances of protocols directly
B. Inherit properties and methods from a class
C. Override methods in a protocol
D. Create a new protocol that includes requirements from one or more existing protocols
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 D
Hint: Protocol inheritance combines requirements from multiple protocols [OK]
Common Mistakes:
Confusing protocol inheritance with class inheritance
Thinking protocols can be instantiated
Believing protocols can override methods
2. Which of the following is the correct syntax to declare a protocol AdvancedProtocol that inherits from BasicProtocol in Swift?
easy
A. protocol AdvancedProtocol: BasicProtocol {}
B. protocol AdvancedProtocol inherits BasicProtocol {}
C. protocol AdvancedProtocol extends BasicProtocol {}
D. protocol AdvancedProtocol implements BasicProtocol {}
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 A
Quick Check:
Protocol inheritance syntax uses colon (:) [OK]
Hint: Use colon (:) to inherit protocols in Swift [OK]
Common Mistakes:
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
3. Given the following code, what will be the output when 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()
medium
A. Info from A
B. Extra from B
C. No output, compile error
D. Info from A\nExtra from B
Solution
Step 1: Understand protocol inheritance and class conformance
Protocol B inherits from A, so class Example must implement both printInfo() and printExtra(). It does.
Step 2: Identify which method is called
The code calls example.printInfo(), which prints "Info from A".
Final Answer:
Info from A -> Option A
Quick Check:
Calling printInfo() prints "Info from A" [OK]
Hint: Match called method to implemented function output [OK]
ElectricVehicle inherits from Vehicle, so conforming types must implement both start() and charge().
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 miss charge() or start(). 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 B
Quick Check:
Implement all inherited protocol methods in class [OK]
Hint: Implement all methods from inherited protocols in your class [OK]
Common Mistakes:
Implementing only one protocol method
Conforming to wrong protocol
Missing methods from inherited protocols
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.