0
0
Swiftprogramming~10 mins

Protocol extensions with default implementations in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protocol extensions with default implementations
Define Protocol
Add Extension with Default Impl
Create Struct/Class conforming Protocol
Call Method
Has Impl?
Run Impl
This flow shows how a protocol defines requirements, extensions add default methods, and conforming types use either their own or the default implementations.
Execution Sample
Swift
protocol Greetable {
    func greet()
}

extension Greetable {
    func greet() {
        print("Hello from default implementation!")
    }
}

struct Person: Greetable {}

let p = Person()
p.greet()
Defines a protocol with a greet method, provides a default greet in an extension, then a struct uses the default greet.
Execution Table
StepActionEvaluationResult
1Define protocol Greetable with greet()Protocol createdGreetable protocol ready
2Add extension to Greetable with default greet()Default method addedDefault greet() prints message
3Define struct Person conforming to GreetablePerson createdPerson conforms to Greetable but no greet() method
4Create instance p of PersonInstance createdp is Person instance
5Call p.greet()Check if Person has greet()No greet() in Person
6Use default greet() from extensionRun default greet()Prints: Hello from default implementation!
💡 Execution stops after default greet() prints message
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
pnilnilnilnilPerson instancePerson instance
Key Moments - 2 Insights
Why does p.greet() call the default implementation even though Person doesn't define greet()?
Because Person conforms to Greetable but does not provide its own greet(), Swift uses the default greet() from the protocol extension as shown in execution_table step 5 and 6.
What happens if Person defines its own greet() method?
If Person defines greet(), that method is called instead of the default. The default is only used when the conforming type lacks its own implementation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what does Swift check when calling p.greet()?
AIf p is nil
BIf Person has its own greet() method
CIf protocol Greetable has any methods
DIf greet() is a static method
💡 Hint
Refer to execution_table step 5 where it checks if Person has greet()
At which step does the default greet() method get executed?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
See execution_table step 6 where default greet() runs
If Person had its own greet() method, how would the execution_table change at step 6?
AStep 6 would call a different protocol method
BStep 6 would be skipped
CStep 6 would call Person's greet() instead of default
DStep 6 would cause a compile error
💡 Hint
Recall key_moments answer about overriding default implementations
Concept Snapshot
protocol ProtocolName {
    func method()
}

extension ProtocolName {
    func method() { /* default code */ }
}

struct Type: ProtocolName {}

// Calling method() on Type uses default if no own implementation

Default implementations provide shared behavior without requiring each type to implement.
Full Transcript
This visual trace shows how Swift protocols can have default method implementations via extensions. First, a protocol named Greetable is defined with a greet() method requirement. Then, an extension adds a default greet() method that prints a message. A struct Person conforms to Greetable but does not implement greet() itself. When we create an instance p of Person and call p.greet(), Swift checks if Person has its own greet() method. Finding none, it uses the default greet() from the protocol extension, printing the message. This demonstrates how default implementations let conforming types share common behavior without writing the method themselves. If Person had its own greet(), that would be called instead of the default. This helps beginners understand protocol extensions as a way to provide reusable default code.