What is a Protocol in Swift?
What is a Swift protocol — requirements, protocol extensions, composition, and protocol-oriented programming, explained with examples.
Expected Interview Answer
A Swift protocol is a blueprint of methods, properties, and other requirements that a class, struct, or enum can adopt and conform to, functioning like an interface but usable by value types as well as reference types.
Unlike a Java interface, a Swift protocol can be adopted by structs and enums, not only classes, because Swift’s type system treats value types as first-class citizens. Protocols can require instance methods, static methods, properties with get/set requirements, and initializers, and Swift additionally supports protocol extensions, which let you provide default implementations for protocol requirements so conforming types get behavior for free. Protocol composition lets a type conform to several protocols at once using the & syntax, giving fine-grained capability grouping similar to role interfaces. Protocol-oriented programming, a paradigm Apple popularized for Swift, favors composing behavior through protocols and extensions over building deep class inheritance hierarchies.
- Works across classes, structs, and enums, not just reference types
- Protocol extensions provide default implementations, reducing boilerplate
- Protocol composition allows precise, combinable capability requirements
- Encourages composition over deep inheritance hierarchies
AI Mentor Explanation
A protocol is like a fielding-position certification that any player type can earn — a bowler, a batsman, or an all-rounder can all be certified Catchable-capable, meaning each guarantees a catch() action, regardless of what kind of player they otherwise are. The certification board (protocol extension) can even provide a default catching technique that any certified player gets for free unless they choose to specialize it. A player can hold several certifications at once, such as Catchable and Runnable, without belonging to one rigid player hierarchy. That flexibility across very different player types is what a Swift protocol provides across classes, structs, and enums.
Step-by-Step Explanation
Step 1
Declare the protocol
Use protocol Name { } to list required methods, properties, and initializers.
Step 2
Adopt in any type
A class, struct, or enum lists the protocol after its type declaration to conform.
Step 3
Implement requirements
The conforming type provides concrete bodies for each protocol requirement, unless a default exists.
Step 4
Add default behavior via extension
Optionally extend the protocol to supply default implementations, reducing repetition across conformers.
What Interviewer Expects
- Correct definition emphasizing structs/enums can conform, not just classes
- Mention of protocol extensions providing default implementations
- Awareness of protocol composition using &
- Understanding of protocol-oriented programming as a Swift design philosophy
Common Mistakes
- Assuming Swift protocols only work with classes like Java interfaces
- Forgetting protocol extensions can supply default implementations
- Not knowing about protocol composition (Protocol1 & Protocol2)
- Confusing a protocol requirement with a stored property (protocols only declare requirements)
Best Answer (HR Friendly)
“A protocol in Swift defines a set of methods and properties that a type promises to implement, and it works with classes, structs, and enums, not just classes like a traditional interface. Swift also lets you extend a protocol to give conforming types default behavior for free, and you can combine several protocols together to describe exactly the capabilities a type needs. This is central to how Swift code favors composing small protocols over building deep inheritance hierarchies.”
Code Example
// Java interface used to illustrate the protocol-style contract
interface Flyable {
void fly();
// Java 8+ default method mirrors a Swift protocol extension’s default impl
default void takeOff() {
System.out.println("Taking off using default sequence");
}
}
class Bird implements Flyable {
public void fly() { System.out.println("Bird flaps wings"); }
// takeOff() inherited for free, like a Swift protocol extension default
}
class Drone implements Flyable {
public void fly() { System.out.println("Drone spins rotors"); }
@Override
public void takeOff() { System.out.println("Drone auto-calibrates before takeoff"); }
}Follow-up Questions
- How do Swift protocol extensions differ from Java default methods?
- What is protocol composition and when would you use Protocol1 & Protocol2?
- Can a Swift protocol require an initializer? How does that work with classes vs structs?
- What is protocol-oriented programming and how does it differ from class-based inheritance?
MCQ Practice
1. Which Swift types can conform to a protocol?
Unlike interfaces in many languages, Swift protocols can be adopted by classes, structs, and enums.
2. What lets a Swift protocol provide default behavior to conforming types?
Extending a protocol with a method body supplies a default implementation that conformers get for free.
3. How do you require a type to conform to two protocols at once in Swift?
Swift supports protocol composition, e.g. func f(x: Protocol1 & Protocol2), to require multiple conformances.
Flash Cards
Swift protocol in one line? — A blueprint of requirements that classes, structs, or enums can adopt and conform to.
What is a protocol extension? — A way to give protocol requirements default implementations, shared by all conformers.
Protocol composition syntax? — Protocol1 & Protocol2, requiring conformance to both at once.
Key difference from Java interfaces? — Swift protocols can be adopted by value types (structs/enums), not only reference types.