A protocol extension adds default implementations of methods or properties to a protocol, so all types conforming to that protocol get shared behavior automatically.
Click to reveal answer
beginner
How do protocol extensions help reduce code duplication?
By providing default method implementations in the protocol extension, multiple types can share the same behavior without writing the same code again.
Click to reveal answer
intermediate
Can a type that conforms to a protocol override a method provided by a protocol extension?
Yes, a conforming type can provide its own implementation to override the default behavior from the protocol extension.
Click to reveal answer
beginner
Example: What will this print?
protocol Greetable {
func greet()
}
extension Greetable {
func greet() { print("Hello from extension") }
}
struct Person: Greetable {}
let p = Person()
p.greet()
It prints: Hello from extension
Because Person conforms to Greetable but does not implement greet(), so it uses the default from the extension.
Click to reveal answer
intermediate
Why might you use protocol extensions instead of base classes?
Protocol extensions allow shared behavior without forcing inheritance, so types can conform to multiple protocols and still share code, making design more flexible.
Click to reveal answer
What does a protocol extension in Swift provide?
ADefault implementations for protocol methods
BInheritance from a base class
CA way to create stored properties
DAutomatic memory management
✗ Incorrect
Protocol extensions provide default implementations for methods or properties declared in the protocol.
If a type conforms to a protocol with an extension method, but also implements that method itself, which version is called?
AIt causes a compile error
BThe protocol extension version
CBoth versions are called
DThe type's own implementation
✗ Incorrect
The type's own implementation overrides the default provided by the protocol extension.
Can protocol extensions add stored properties to protocols?
AYes, always
BNo, only computed properties or methods
COnly if marked @objc
DOnly in classes
✗ Incorrect
Protocol extensions cannot add stored properties, only computed properties or methods.
Why are protocol extensions useful for shared behavior?
AThey enforce strict type checking
BThey improve runtime speed
CThey allow code reuse without inheritance
DThey replace all classes
✗ Incorrect
Protocol extensions let you share code across types without using class inheritance.
Which Swift feature lets you add default method implementations to protocols?
AProtocol extensions
BGenerics
CClosures
DOptionals
✗ Incorrect
Protocol extensions are the feature that allows default implementations in protocols.
Explain how protocol extensions provide shared behavior in Swift.
Think about how you can write one method once and have many types use it.
You got /4 concepts.
Describe the difference between protocol extensions and class inheritance for sharing code.
Consider how protocols let you share behavior without forcing a class hierarchy.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using protocol extensions in Swift?
easy
A. To store data inside protocols
B. To add shared behavior to multiple types without repeating code
C. To create new classes that inherit from protocols
D. To prevent types from conforming to protocols
Solution
Step 1: Understand protocol extensions
Protocol extensions allow adding default method or property implementations to protocols.
Step 2: Identify the benefit
This lets many types share behavior without rewriting the same code.
Final Answer:
To add shared behavior to multiple types without repeating code -> Option B
Quick Check:
Protocol extensions = shared behavior [OK]
Hint: Protocol extensions add default behavior to many types [OK]
Common Mistakes:
Thinking protocols can store data
Confusing protocol extensions with inheritance
Believing protocol extensions prevent conformance
2. Which of the following is the correct syntax to add a default method greet() to a protocol Greeter using an extension?
Person implements speak(), overriding the default. This is allowed and correct.
Final Answer:
No error; output is 'Hello' -> Option A
Quick Check:
Override default method by implementing in type [OK]
Hint: Implementing method in type overrides default extension [OK]
Common Mistakes:
Thinking default methods cause errors
Trying to use 'override' keyword in struct
Believing protocol methods must be optional
5. You want to add a shared method describe() to multiple unrelated types Cat and Dog without repeating code. Which approach using protocol extensions is best?
hard
A. Add a global function describe() that takes Any type
B. Add describe() method separately in Cat and Dog structs
C. Use class inheritance from a base class with describe() method
D. Create a protocol Animal with describe() and add default implementation in extension; make Cat and Dog conform
Solution
Step 1: Identify shared behavior need
Both Cat and Dog need the same describe() method without repeating code.
Step 2: Use protocol with extension
Define protocol Animal with describe(), then provide default describe() in protocol extension. Make Cat and Dog conform to Animal.
Final Answer:
Create a protocol Animal with describe() and add default implementation in extension; make Cat and Dog conform -> Option D
Quick Check:
Protocol extension = shared behavior for unrelated types [OK]
Hint: Use protocol + extension for shared methods across types [OK]
Common Mistakes:
Repeating code in each type
Using class inheritance when types are unrelated
Using global functions losing type safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.