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.
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.
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.
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.