DevPrep

All topics › Object-oriented programming (OOP)

Encapsulation

Junior

Essence

Encapsulation means bundling data together with the behaviour that works on it inside one type and controlling access to that state. The outside sees only the contract (the public interface), while invariants ("balance is never negative") are protected inside the type. This localizes change: you can rewrite how data is stored without touching clients.

What it looks like in Swift

Access levels from most restrictive to most open: private (inside the declaration and its extensions in the same file), fileprivate, internal (the default, whole module), package (several modules of one package, Swift 5.9), public (visible from other modules), open (can also be subclassed/overridden outside the module). private(set) lets everyone read but only the type itself write.

import Foundation

struct BankAccount {
    private(set) var balance: Decimal = 0
    private var history: [Decimal] = []

    mutating func deposit(_ amount: Decimal) {
        precondition(amount > 0)
        balance += amount
        history.append(amount)
    }
}

var account = BankAccount()
account.deposit(100)
print(account.balance)      // 100
// account.balance = 1_000  // error: setter is private

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