Skip to content
BENIBIX Learn

Encapsulation vs Abstraction

Encapsulation and abstraction both hide things, but not the same things. Encapsulation hides an object's data, so only its methods can change it. Abstraction hides how a job is done. You see only a simple way to use it.

Development🧭 Programming Paradigms

Encapsulation

Hiding an object's data. Other code must use safe methods to change it.

Encapsulation means keeping an object's data inside it. Other code can only change it through the object's own methods. The data is hidden, and only safe doors are left open. That way the object can check every change, like saying no to a negative balance. Code outside does not need to know how the object stores things. So the inside can change later without breaking the rest.

Example
Emma's Account class hides its balance. Other code must call deposit(), which says no to amounts below zero.
Why it matters
It stops other code from breaking an object by mistake.
Full entry
Development🧭 Programming Paradigms

Abstraction (Programming)

Showing only what you need to use a thing. The details stay hidden.

Abstraction means showing only what you need to use a thing. The messy details stay hidden behind it. A sendEmail function hides the servers, ports and retries inside it. You think in big, simple steps instead of tiny ones. Every function, class and library is a layer of it. Good ones let you forget the details; bad ones leak them at the worst time.

Example
Emma calls save(order) and the order is stored. She never sees the SQL, the connection or the retry rules inside it.
Why it matters
It lets people build huge programs without holding each detail in mind.
Full entry

In one line

Encapsulation
Hiding an object's data. Other code must use safe methods to change it.
Abstraction (Programming)
Showing only what you need to use a thing. The details stay hidden.