Hello All

I have little confusion between Abstraction and encapsulation.

Abstraction means Giving only essential things and hiding unnecessary details.

Encapsulation means binding the data members and methods together in a capsule form to avoid accidental changes to data from external users.

Now both uses hiding of data.

What is the difference of hiding of data in between them?

Encapsulation is wrapping data into single unit (e.g A class)
Abstraction is hiding unessential parts and showing only essential data.

Abstraction means Giving only essential things and hiding unnecessary details.

Implementation details are often hidden away behind a more intuitive interface in abstraction, but that's not required. Abstraction is the process of matching the representation of data or algorithms to their meaning within a program. It's a method of implementing "code for the application, not the computer".

An example of abstraction is std::map<A,B>. We only care about uniquely mapping A to B and being able to perform fast lookups of B based on A. There's really no reason to know that std::map is implemented as a balanced search tree, and we certainly don't want to worry about managing the details of a search tree when we really just want a mapping.

The std::map abstraction gives us the ability to write what we meant in terms of the application rather than lower level junk that makes the computer happy and just obscures what the application is really doing. This is abstraction.

Encapsulation means binding the data members and methods together in a capsule form to avoid accidental changes to data from external users.

Encapsulation is the bundling of related algorithms and data. It's possible (and not entirely uncommon) to use encapsulation without any kind of data hiding:

struct point {
    int x;
    int y;

    point(int x, int y);
};

struct line {
    point a;
    point b;

    line(point a, point b);
    double distance() const;
};

This bundling makes for convenient interfaces as well as simplifies the task of abstraction and data hiding.

Now both uses hiding of data.

What is the difference of hiding of data in between them?

Data hiding is a separate concept (one of the key concepts of object orientation) that's not unique to either abstraction or encapsulation. Both abstraction and encapsulation can make use of data hiding, but neither are required to include it.

Narue's answer is good, of course. But on a less formal basis, I see encapsulation and abstraction as sort of two sides of the same coin. In both cases, you usually hide away the details that the user of your library or class doesn't need to know about (hiding data and/or implementation details (like helper functions)). In a sense, encapsulation is asking yourself: What are the things the user shouldn't see? and abstraction is asking yourself: What are the only things the user should see? Most software components have things the user really shouldn't see (e.g. see the PImpl idiom) and things that are part of the interface of this software component (see "programming by contract in OOP"). In an ideal world, the split is clear, in the real world, the line is blurred, so it makes sense to think of both sides of the question (should this thing be hidden? is this thing part of the (abstract) interface?).

Edited 5 Years Ago by mike_2000_17: n/a

This question has already been answered. Start a new discussion instead.