DevPrep

All topics › Object-oriented programming (OOP)

Abstraction

Junior

Essence

Abstraction means extracting what matters and hiding implementation details behind a clear contract. We say "image loader", not "URLSession plus disk cache plus decoding". Each level of abstraction lets you reason about a task without holding the lower levels in your head. It is the main tool for fighting complexity.

In Swift an abstraction is a protocol

Swift has no abstract classes, so the contract is described by a protocol and implementations are any types (most often structs).

import Foundation

protocol ImageLoader {
    func load(_ url: URL) async throws -> Data
}

struct NetworkImageLoader: ImageLoader {
    func load(_ url: URL) async throws -> Data {
        try await URLSession.shared.data(from: url).0
    }
}

struct CachedImageLoader: ImageLoader {
    let base: any ImageLoader
    func load(_ url: URL) async throws -> Data {
        // check cache, otherwise delegate to `base`
        try await base.load(url)
    }
}

// Client code knows only the abstraction:
func makeLoader() -> some ImageLoader {
    CachedImageLoader(base: NetworkImageLoader())
}

The client works with ImageLoader and has no idea there is a network and a cache behind it. The implementation can be swapped for a stub in tests, and some ImageLoader hides the concrete type even in the signature.

Abstraction vs encapsulation

What the interviewer looks for

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store