Abstract functions in Swift Language
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Swift does not have an abstract keyword like Java or C#. Instead, Swift uses protocols to define abstract interfaces and protocol extensions to provide default implementations. A method declared in a protocol without a body is effectively abstract — any conforming type must implement it. For class hierarchies that need abstract base classes, Swift developers use protocols, fatalError() in base class methods, or a combination of both. This article covers all the patterns for achieving abstract function behavior in Swift.
Protocols as Abstract Interfaces
Protocol methods without bodies are abstract — conforming types must provide implementations.
Protocol Extensions for Default Implementations
Abstract Base Class with fatalError()
When you need class inheritance with abstract methods:
The fatalError() approach works but has a major drawback: forgetting to override speak() in a subclass is not caught until runtime.
Protocol + Class Combination (Best Practice)
Combine a protocol for the abstract contract with a base class for shared state:
The protocol enforces draw() and bounds at compile time. The base class provides shared functionality (show(), hide()).
Associated Types for Generic Abstractions
Common Pitfalls
- Relying on
fatalError()for abstract enforcement:fatalError()crashes at runtime, not compile time. A subclass that forgets to override the method compiles successfully but crashes when called. Prefer protocols, which enforce implementation at compile time. - Putting abstract methods in protocol extensions: A method defined in a protocol extension provides a default implementation. If a conforming type does not override it, the default silently runs. Only declare methods in the protocol body (without extension) if they must be implemented by every conforming type.
- Confusing protocol conformance with class inheritance: Protocols define what a type can do (interface). Classes define what a type is (hierarchy). In Swift, prefer protocols for abstraction and use class inheritance only when shared state or identity semantics are needed.
- Making protocols too large: A protocol with 20 required methods is hard to conform to. Split large protocols into smaller, focused ones (Interface Segregation Principle). Use protocol composition (
Drawable & Animatable) to combine them. - Forgetting that protocol methods use static dispatch in extensions: Methods declared in a protocol extension (not in the protocol itself) use static dispatch. This means the extension's version is called based on the variable's declared type, not the runtime type. Override behavior requires the method to be declared in the protocol body.
Summary
- Swift has no
abstractkeyword — use protocols to define abstract interfaces - Protocol methods without bodies must be implemented by conforming types (compile-time enforcement)
- Protocol extensions provide default implementations that conforming types can override
- Use
fatalError()in base class methods as a last resort — it only fails at runtime - Combine protocols (for abstraction) with base classes (for shared state) for the best of both worlds
- Prefer protocols over class hierarchies — Swift is protocol-oriented by design
Related reading
- Access Asset Catalog programmatically
- Access Container View Controller from Parent iOS
- Access Container View Controller from Parent iOS
- Accessing an SQLite Database in Swift
- Accessing Kotlin extension functions from Java
- Accessing localhostport from Android emulator
- Accessing UI thread handler from a service
- Accessing ViewModel field in SwiftUI using Xcode 12 Accessing State's value outside of being installed on a View
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.