What is the precise difference between encapsulation and abstraction?
Encapsulation means hiding the internal details or mechanics of how an object does something. Abstraction is outer layout in terms of design. For Example: - Outer Look of a iPhone, like it has a display screen. Encapsulation is inner layout in terms of implementation.
Encapsulation: Wrapping code and data together into a single unit. Class is an example of encapsulation, because it wraps the method and property. Abstraction: Hiding internal details and showing functionality only. Abstraction focus on what the object does instead of how it does.
Explanation: Abstraction is hiding the complex code. For example, we directly use cout object in C++ but we don't know how is it actually implemented. Encapsulation is data binding, as in, we try to combine a similar type of data and functions together.
Abstraction hides complexity by giving you a more abstract picture, a sort of 10,000 feet view, while Encapsulation hides internal working so that you can change it later. In other words, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.
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, distance
encapsulates 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 qsort
function to sort data:
The thing about qsort
is 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 of qsort
always 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. qsort
therefore 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 the 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With