Encapsulation hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.
Abstraction is used to hide something too, but in a higher degree (class, interface). Clients who use an abstract class (or interface) do not care about what it was, they just need to know what it can do.
Answer from phnkha on Stack OverflowEncapsulation hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.
Abstraction is used to hide something too, but in a higher degree (class, interface). Clients who use an abstract class (or interface) do not care about what it was, they just need to know what it can do.
This image sums pretty well the difference between both:

Source here
oop - Difference between abstraction and encapsulation? - Stack Overflow
oop - encapsulation vs abstraction real world example - Stack Overflow
Give one real time example to understand both "Abstraction" & "Encapsulation" ?
Understanding Encapsulation and Abstraction real use case
encapsulation hides information from the caller, abstraction hides information from the callee.
when you encapsulate, you make sure the caller of a function knows as little about what the function does as it possibly can while still supporting reasonable use.
when you abstract, you make sure the function itself knows as little about how it's used as it possibly can while still allowing implementation of the desired behavior.
well encapsulated functions provide ergonomic interfaces. minimizing the surface area at the call sites by minimizing the info taken in and returned makes it easier to drop a call in to multiple different contexts without the slog that comes from a badly tangled interface that requires multiple loc to setup before or cleanup after a function call.
well abstracted functions provide more reliable behavior. limiting the power of a functions implementation by limiting its knowledge of its inputs or the contexts it's called in makes it safer to call in a wider variety of settings, simply because a less powerful (read knowledgable) function just has less opportunity to do surprising or unwelcome things.
abstraction is definitely not just a best practice, and there's hard math you can do to prove to yourself the concrete difference between different ways of writing the same code in more or less abstract ways, in terms of how many ways you can write it incorrectly. abstraction brings that number down in meaningful ways.
encapsulation is in my understanding a fuzzier idea. it's not inherent to computation, but emergent in specific programming models and languages. i can't think of a setting among programmers where it'd be contentious to say it has meaningful practical impact, but the most "precise" measure i can think of is roughly how many places in a code-base you'd have to change for a single desired change in behavior.
More on reddit.comIs encapsulation possible without abstraction?
Do abstraction and encapsulation improve code readability?
What is the main goal of encapsulation?
They both have extremely similar definitions. I keep getting conflicting information from the internet that varies from "encapsulation is a subset of abstraction" to "encapsulation is a completely different thing than abstraction".
Most answers here focus on OOP but encapsulation begins much earlier:
Every function is an encapsulation; in pseudocode:
point x = { 1, 4 } point y = { 23, 42 } numeric d = distance(x, y)Here,
distanceencapsulates the calculation of the (Euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple.Abstraction is the process of generalisation: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C’s
qsortfunction to sort data:The thing about
qsortis that it doesn't care about the data it sorts — in fact, it doesn’t know what data it sorts. Rather, its input type is a typeless pointer (void*) which is just C’s way of saying “I don't care about the type of data” (this is also called type erasure). The important point is that the implementation ofqsortalways stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type.qsorttherefore expects the user to provide said compare function as a function argument.
Encapsulation and abstraction go hand in hand so much so that you could make the point that they are truly inseparable. For practical purposes, this is probably true; that said, here’s an encapsulation that’s not much of an abstraction:
class point {
numeric x
numeric y
}
We encapsulate the point’s coordinate, but we don’t materially abstract them away, beyond grouping them logically.
And here’s an example of abstraction that’s not encapsulation:
T pi<T> = 3.1415926535
This is a generic variable pi with a given value (π), and the declaration doesn’t care about the exact type of the variable. Admittedly, I’d be hard-pressed to find something like this in real code: abstraction virtually always uses encapsulation. However, the above does actually exist in C++(14), via variable templates (= generic templates for variables); with a slightly more complex syntax, e.g.:
template <typename T> constexpr T pi = T{3.1415926535};
Many answers and their examples are misleading.
Encapsulation is the packing of "data" and "functions operating on that data" into a single component and restricting the access to some of the object's components.
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition.
Abstraction is a mechanism which represent the essential features without including implementation details.
Encapsulation:-- Information hiding.
Abstraction:-- Implementation hiding.
Example (in C++):
class foo{
private:
int a, b;
public:
foo(int x=0, int y=0): a(x), b(y) {}
int add(){
return a+b;
}
}
Internal representation of any object of foo class is hidden outside of this class. --> Encapsulation.
Any accessible member (data/function) of an object of foo is restricted and can only be accessed by that object only.
foo foo_obj(3, 4);
int sum = foo_obj.add();
Implementation of method add is hidden. --> Abstraction.
Encapsulation is a way to achieve "information hiding". So, following your example, you don't "need to know the internal working of the mobile phone to operate" with it. You have an interface to use the device behaviour without knowing implementation details.
Abstraction, on the other side, can be explained as the capability to use the same interface for different objects (i.e. devices, mobile phones). Different implementations of the same interface can exist. Details are hidden by encapsulation.
Abstraction : you'll never buy a "device", but always buy something more specific : iPhone, GSII, Nokia 3310... Here, iPhone, GSII and N3310 are concrete things, device is abstract.
Encapsulation : you've got several devices, all of them have got a USB port. You don't know what kind of printed circuit there's back, you just have to know you'll be able to plug a USB cable into it.
Abstraction is a concept, which is allowed by encapsulation. My example wasn't the best one (there's no real link between the two blocks).
You can do encapsulation without using abstraction, but if you wanna use some abstraction in your projects, you'll need encapsulation.