Abstraction vs. Encapsulation: Still Confused? Let This Article Clear It Up
When studying object-oriented programming (OOP), you inevitably encounter a major stumbling block.
That stumbling block is abstraction and encapsulation.
These concepts can sound almost identical. If asked in an interview, “What’s the difference between abstraction and encapsulation?”, it’s easy to give a vague answer.
Let’s start with the conclusion.
Abstraction is a design perspective that decides “what to show,” while encapsulation is an implementation perspective that decides “how to hide it.”
Grasping this one sentence gets you halfway there. Let’s make the rest stick with examples.
What Is Abstraction?
Abstraction hides complex internals and exposes only the essential parts.
Think about a car. When driving, we don’t need to know how the engine ignites the fuel.
The steering wheel, pedals, and gear: knowing this interface is enough to drive.
In other words, abstraction is deciding what to expose to the driver.
In code, it can look like this.
protocol Coffee {
func brew()
}
struct Americano: Coffee {
func brew() {
print("Brew an Americano")
}
}
let menu: Coffee = Americano()
menu.brew()
// Output: Brew an Americano
The caller knows only the essence, brew(). How the beans are ground or what temperature the water reaches is irrelevant.
That is abstraction.
What Is Encapsulation?
Encapsulation bundles data with the functions that operate on it and prevents direct external access.
The key ideas are hiding and protection.
Think of a bank account. It would be disastrous if external code could freely change the balance.
So the balance is hidden and can be changed only through defined channels such as deposits and withdrawals.
class Account {
private var balance = 0
func deposit(_ amount: Int) {
guard amount > 0 else { return }
balance += amount
}
func getBalance() -> Int {
return balance
}
}
let acc = Account()
acc.deposit(1000)
print(acc.getBalance())
// Output: 1000
We blocked balance with private. Externally, the value can be changed only through the defined gateway, deposit.
Hiding internals and controlling access this way is encapsulation.
So What Is the Decisive Difference?
Here’s the summary.
| Category | Abstraction | Encapsulation |
|---|---|---|
| Purpose | Hide complexity and expose only the essence | Hide and protect data |
| Perspective | Design perspective (what) | Implementation perspective (how) |
| Question | “What should I show?” | “How should I hide it?” |
| Means | Interfaces, abstract classes | Access control modifiers (such as private) |
Let’s pinpoint the most confusing part.
The confusion comes from the fact that both involve hiding something.
But they hide different things.
Abstraction hides complex processes (logic) to simplify usage. Encapsulation, by contrast, hides data (state) to protect it safely.
When Should You Focus on Which?
In practice, you use both together. The difference is where you focus.
- When designing a new feature’s interface → focus on abstraction
- When preserving the integrity of a class’s internal state → focus on encapsulation
- When deciding which API to expose to collaborators → abstraction
- When managing values safely without bugs → encapsulation
This Is How They Ask in Interviews
Q. Explain the difference between abstraction and encapsulation.
The key is the difference in perspective. Abstraction is a design concept that hides unnecessary details and reveals only the essence, while encapsulation is an implementation concept that hides data and restricts access. It is also useful to add that abstraction is realized through interfaces and encapsulation through access control modifiers.
Q. Why is encapsulation necessary?
If external code changes an object’s state directly, data integrity can easily break. Explain that allowing state changes only through defined methods centralizes validation logic and makes maintenance and debugging easier.
These concepts are partners, not opposites. If today’s examples made them clearer, ask yourself once when writing your next piece of code: “What am I showing, and what am I hiding?” OOP will become much clearer.

