Bird
Raised Fist0
Swiftprogramming~10 mins

Protocol inheritance in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Protocol inheritance
Define Base Protocol
↓
Define Derived Protocol: inherits Base
↓
Create Struct/Class: conforms to Derived
↓
Implement all requirements from Base + Derived
↓
Use instance as Derived or Base Protocol
Protocol inheritance means one protocol can require everything from another, so a type conforming to the child protocol must implement all requirements from both.
Execution Sample
Swift
protocol A {
    func foo()
}

protocol B: A {
    func bar()
}

struct S: B {
    func foo() { print("foo") }
    func bar() { print("bar") }
}

let s = S()
s.foo()
s.bar()
Defines protocol A, then B inherits A. Struct S conforms to B, so it must implement foo() and bar(). Then calls both methods.
Execution Table
StepActionEvaluationResult
1Define protocol A with foo()No outputProtocol A ready
2Define protocol B inheriting A, adds bar()No outputProtocol B ready (inherits foo())
3Define struct S conforming to BNo outputS must implement foo() and bar()
4Implement foo() in SPrints 'foo' when calledfoo() implemented
5Implement bar() in SPrints 'bar' when calledbar() implemented
6Create instance s of SNo outputInstance s created
7Call s.foo()Calls foo()Output: foo
8Call s.bar()Calls bar()Output: bar
9End of programNo more actionsExecution stops
💡 Program ends after calling both foo() and bar() on instance s
Variable Tracker
VariableStartAfter 6After 7After 8Final
sundefinedinstance of Sinstance of Sinstance of Sinstance of S
Key Moments - 3 Insights
Why does struct S need to implement foo() even though it only says it conforms to protocol B?
Because protocol B inherits protocol A, it requires all methods from A too. So S must implement foo() from A and bar() from B, as shown in steps 3-5 of the execution_table.
What happens if S does not implement foo()?
The code will not compile because S does not fulfill all requirements of protocol B, which includes foo() from protocol A. This is implied in step 3 where S must implement both methods.
Can we use instance s as type A or B?
Yes, since S conforms to B which inherits A, s can be used as either protocol type. This is the purpose of protocol inheritance shown in the flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed when s.foo() is called at step 7?
A"foo"
B"bar"
CNothing
DError
💡 Hint
Check row 7 in execution_table where s.foo() is called and output is "foo"
At which step does struct S become required to implement both foo() and bar()?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look at step 3 in execution_table where S is defined conforming to B and must implement both methods
If protocol B did not inherit protocol A, what would change in the execution_table?
AS would need to implement foo() and bar() anyway
BS would only need to implement bar(), not foo()
CS could implement neither method
DProgram would not compile
💡 Hint
Protocol inheritance means B requires A's methods; without it, only B's own methods are required (see concept_flow)
Concept Snapshot
Protocol inheritance syntax:
protocol B: A { ... }
Means B includes all requirements of A.
A type conforming to B must implement all methods from A and B.
Use inheritance to build on protocols.
Allows flexible, reusable interfaces.
Full Transcript
This visual trace shows how protocol inheritance works in Swift. First, protocol A is defined with a method foo(). Then protocol B inherits A and adds method bar(). A struct S conforms to B, so it must implement both foo() and bar(). The execution table walks through defining protocols, struct, implementing methods, creating an instance, and calling methods. The variable tracker shows the instance s is created and used. Key moments clarify why S must implement foo() even though it only says it conforms to B, because B inherits A. The quiz questions check understanding of method calls, implementation requirements, and effects of inheritance. The snapshot summarizes the syntax and behavior of protocol inheritance. This helps beginners see step-by-step how protocol inheritance requires implementing all inherited methods.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Create a new protocol that includes requirements from one or more existing protocols -> Option D
  4. Quick Check:

    Protocol inheritance = create combined protocols [OK]
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

  1. Step 1: Recall Swift protocol inheritance syntax

    In Swift, a protocol inherits from another by listing the parent protocol after a colon (:).
  2. Step 2: Match syntax with options

    Only protocol AdvancedProtocol: BasicProtocol {} uses the correct syntax: protocol NewProtocol: ExistingProtocol {}.
  3. Final Answer:

    protocol AdvancedProtocol: BasicProtocol {} -> Option A
  4. 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

  1. Step 1: Understand protocol inheritance and class conformance

    Protocol B inherits from A, so class Example must implement both printInfo() and printExtra(). It does.
  2. Step 2: Identify which method is called

    The code calls example.printInfo(), which prints "Info from A".
  3. Final Answer:

    Info from A -> Option A
  4. Quick Check:

    Calling printInfo() prints "Info from A" [OK]
Hint: Match called method to implemented function output [OK]
Common Mistakes:
  • Confusing which method is called
  • Expecting both methods to print automatically
  • Thinking protocol inheritance affects method output
4. What is wrong with the following Swift code?
protocol P1 {
    func method1()
}
protocol P2: P1 {
    func method2()
}
class MyClass: P2 {
    func method2() {
        print("Method 2")
    }
}
medium
A. P2 cannot inherit from P1
B. method2() should not be implemented in MyClass
C. MyClass must implement method1() because P2 inherits from P1
D. MyClass should be a protocol, not a class

Solution

  1. 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.
  2. Step 2: Check implemented methods in MyClass

    MyClass implements only method2() but misses method1(), causing a compile error.
  3. Final Answer:

    MyClass must implement method1() because P2 inherits from P1 -> Option C
  4. Quick Check:

    Inherited protocol methods must be implemented [OK]
Hint: Implement all inherited protocol methods in conforming class [OK]
Common Mistakes:
  • Ignoring inherited protocol requirements
  • Thinking only new protocol methods must be implemented
  • Confusing class and protocol roles
5. You have two protocols:
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?
hard
A. class ElectricCar: ElectricVehicle { func start() { print("Starting") } }
B. class ElectricCar: ElectricVehicle { func start() { print("Starting") } func charge() { print("Charging") } }
C. class ElectricCar: Vehicle { func charge() { print("Charging") } }
D. class ElectricCar: ElectricVehicle { func charge() { print("Charging") } }

Solution

  1. Step 1: Understand protocol inheritance requirements

    ElectricVehicle inherits from Vehicle, so conforming types must implement both start() and charge().
  2. 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.
  3. Final Answer:

    class ElectricCar: ElectricVehicle { func start() { print("Starting") } func charge() { print("Charging") } } -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard