Explain protocols and how extensions provide default implementations
Tests behavior sharing without inheritance. A strong answer defines protocols as requirements, shows extensions injecting default implementations, and contrasts this with base-class inheritance.
WHAT THIS TESTS: Even at senior levels, interviewers use this question to filter for shallow knowledge. They want to know if you see protocols as mere interfaces or as the foundation of Swift's type system. The question specifically probes whether you understand that protocol extensions enable code reuse across value types and reference types without forcing an inheritance hierarchy, which is the essence of Protocol-Oriented Programming.
A GOOD ANSWER COVERS: First, define a protocol as a blueprint that declares requirements like properties, methods, and initializers without implementing them. Second, explain that a protocol extension uses the extension keyword on a protocol to provide a default implementation for those requirements. Third, emphasize the power: any struct, enum, or class that conforms gets that behavior for free, and you can layer multiple extensions for conditional conformance. Fourth, contrast this with an abstract base class where reuse requires inheritance, which is unavailable to structs and enums. Fifth, mention that this promotes composition over inheritance and enables retroactive modeling, meaning you can add behavior to existing types.
COMMON WRONG ANSWERS: A major red flag is describing a protocol as a class or reference type. Another is claiming that protocol extensions can add stored properties; they cannot, only computed properties and methods. Some candidates confuse this with Objective-C protocols and say default implementations require the objc attribute, which is false. Others say extensions override behavior like subclassing, when in reality the static dispatch rules mean you must be careful about which implementation is called through a protocol existential.
LIKELY FOLLOW-UPS: The interviewer may ask what happens when a conforming type provides its own implementation versus the extension default, which tests your understanding of static versus dynamic dispatch. They might ask where you would still choose subclassing over protocols, or how conditional conformance works in protocol extensions. A senior candidate should also be ready to discuss the Self and associated type requirements and how extensions interact with them.
ONE CONCRETE EXAMPLE: Imagine a Logger protocol with a method named log that accepts a string. You write an extension on Logger that provides a default implementation printing a prefixed message. Now both a struct NetworkManager and a struct DiskCache can conform to Logger and immediately log without duplicating code. If NetworkManager needs custom behavior, it simply provides its own log method, shadowing the default for direct calls but not always through the protocol witness table.
Read the original → docs.swift.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.