0
0
Swiftprogramming~10 mins

Protocol extensions for shared behavior in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protocol extensions for shared behavior
Define Protocol
Add Extension with Default Methods
Create Struct/Class conforming Protocol
Call Method on Instance
Use Default Implementation if no override
Override Possible in Conforming Type
This flow shows how a protocol is defined, extended with default behavior, and how conforming types use or override that behavior.
Execution Sample
Swift
protocol Greetable {
    func greet()
}

extension Greetable {
    func greet() { print("Hello from protocol extension") }
}

struct Person: Greetable {}

let p = Person()
p.greet()
Defines a protocol with a greet method, provides a default greet implementation in an extension, then calls greet on a conforming struct instance.
Execution Table
StepActionEvaluationResult
1Define protocol Greetable with greet()Protocol createdGreetable protocol exists
2Extend Greetable with default greet()Default method addedgreet() prints 'Hello from protocol extension'
3Define struct Person conforming to GreetablePerson createdPerson conforms to Greetable, no greet() override
4Create instance p of PersonInstance createdp is Person instance
5Call p.greet()No override found, use extension methodPrints: Hello from protocol extension
💡 Method call completed, default implementation used because Person has no override
Variable Tracker
VariableStartAfter Step 4After Step 5
pnilPerson instance createdPerson instance remains unchanged
Key Moments - 2 Insights
Why does p.greet() print the message even though Person has no greet() method?
Because the protocol extension provides a default implementation of greet(), which is used when the conforming type does not override it (see execution_table step 5).
Can Person provide its own greet() method?
Yes, if Person defines its own greet(), that method overrides the default from the protocol extension (not shown here but implied by the flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 5 when p.greet() is called?
APerson's own greet() method is called
BCompilation error because greet() is missing
CDefault greet() from protocol extension is called
DNothing happens
💡 Hint
See execution_table row 5: No override found, default method used
According to variable_tracker, what is the state of variable p after step 4?
Ap is nil
Bp is a Person instance
Cp is a Greetable protocol type
Dp is undefined
💡 Hint
Check variable_tracker row for p after step 4
If Person had its own greet() method, how would step 5 change in the execution_table?
AIt would call Person's greet() instead of the default
BIt would cause a runtime error
CIt would still call the default greet()
DIt would cause a compilation 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 instance uses default unless overridden
Full Transcript
This example shows how Swift protocols can be extended to provide default behavior. First, a protocol named Greetable is defined with a greet() method requirement. Then, an extension adds a default implementation of greet() that prints a message. A struct Person conforms to Greetable but does not implement greet() itself. When we create an instance of Person and call greet(), the default implementation from the protocol extension runs, printing the message. This demonstrates how protocol extensions allow shared behavior without requiring every conforming type to implement the method. If Person provided its own greet(), that would override the default. The execution table traces each step from defining the protocol to calling the method, and the variable tracker shows the state of the instance. The quiz questions test understanding of default method usage and overriding.