understanding of data abstraction, information hiding and encapsulation

S

subramanian100in

Is my following understanding correct ?

Data abstraction means providing the interface - that is, the set of
functions that can be called by the user of a class.

Information hiding means mentioning the class members(functions,
typedefs, data) under the access control labels : public, protected,
private.

Encapsulation means providing the implementation of class member
functions and the implementation is hidden except for the inline
functions which can be present inside the class definition or the
header file which are visible to the user of a class.

Kindly correct me if my understanding is wrong. Wherever I have gone
wrong, please provide an example C++ class so that I can understand.

Also, does the process identification of class, its member functions
and data members, class objects, fall under data abstraction ? If not,
what is it called.

Kindly explain.

Thanks
V.Subramanian
 
K

Kai-Uwe Bux

Is my following understanding correct ?

Data abstraction means providing the interface - that is, the set of
functions that can be called by the user of a class.

Information hiding means mentioning the class members(functions,
typedefs, data) under the access control labels : public, protected,
private.

Encapsulation means providing the implementation of class member
functions and the implementation is hidden except for the inline
functions which can be present inside the class definition or the
header file which are visible to the user of a class.

Encapsulation means that the state of an object can be manipulated by client
code only going through the public accessor methods. It is unrelated to
function implementation being inline or published in a header file.
Templated code usually goes into header files (since most compilers do not
support the export keyword). That does not break encapsulation at all.

Headers are meant for the compiler to read and are no substitute for
documentation. A programmer relying, e.g., on a sort function being stable
(which information may have been obtained by looking at the source and
discovering that heap sort is used) when the function is not documented as
being stable is in very deep waters.
Kindly correct me if my understanding is wrong. Wherever I have gone
wrong, please provide an example C++ class so that I can understand.

class Window {

int lower_x, lower_x, upper_x, upper_y;
// ...

public:

move ( int right, int up ) {
upper_x += right;
lower_x += right;
upper_y += up;
lower_y += up;
// some more
}

// ...

};

This class provides encapsulation since you cannot access its internals from
the outside directly.

Also, does the process identification of class,

Huh? never heard of that.
its member functions and data members, class objects, fall under
data abstraction?

Have you tried looking up a definition of data abstraction? Do those terms
match?
If not, what is it called.

I guess, it's called, in turn, "process information", "member
functions", "data members", and "class objects".

Why do you have a need for a term that is less specific? Only if we know the
purpose, we can suggest an appropriate word. Otherwise, I would suggests
the term "something".



Best

Kai-Uwe Bux
 
G

Guest

Is my following understanding correct ?

Data abstraction means providing the interface - that is, the set of
functions that can be called by the user of a class.
Yes.

Information hiding means mentioning the class members(functions,
typedefs, data) under the access control labels : public, protected,
private.

Encapsulation means providing the implementation of class member
functions and the implementation is hidden except for the inline
functions which can be present inside the class definition or the
header file which are visible to the user of a class.

The way I see it information hiding and encapsulation are more or less
the same thing, and it means that the user of the interface does not
know (or at least do not need to know) how the functionality provided by
the interface is implemented.

What this means is that when you have defined the interface and written
the code to support it a user should be able to take the code and start
using it. Later you should be able to completely replace the
implementation with a new one that provide the same interface and the
user should not have to make any changes to his/her code.
Kindly correct me if my understanding is wrong. Wherever I have gone
wrong, please provide an example C++ class so that I can understand.

A good example is the use of inheritance to define an interface and then
allow different implementations in derived classes:

#include <iostream>

class Twice
{
public:
virtual unsigned int twice(unsigned int i) = 0;
};

class ShiftTwice : public Twice
{
public:
unsigned int twice(unsigned int i)
{
return i << 1;
}
};

class MultiplyTwice : public Twice
{
public:
unsigned int twice(unsigned int i)
{
return 2 * i;
}
};

int main()
{
Twice* ss = new ShiftTwice();
Twice* ms = new MultiplyTwice();

std::cout << ss->twice(4) << "\n";
std::cout << ms->twice(4) << "\n";

delete ss;
delete ms;
}

Since both ShiftTwice and MultiplyTwice have the same interface they can
be used interchangeably as long as we access them through a pointer (or
reference) to Twice.
Also, does the process identification of class, its member functions
and data members, class objects, fall under data abstraction ? If not,
what is it called.

I am not sure what you are talking about, perhaps runtime type
information, but that have nothing to do with any of the above.
 
S

Saeed Amrollahi

Is my following understanding correct ?

Data abstraction means providing the interface - that is, the set of
functions that can be called by the user of a class.

Information hiding means mentioning the class members(functions,
typedefs, data) under the access control labels : public, protected,
private.

Encapsulation means providing the implementation of class member
functions and the implementation is hidden except for the inline
functions which can be present inside the class definition or the
header file which are visible to the user of a class.

Kindly correct me if my understanding is wrong. Wherever I have gone
wrong, please provide an example C++ class so that I can understand.

Also, does the process identification of class, its member functions
and data members, class objects, fall under data abstraction ? If not,
what is it called.

Kindly explain.

Thanks
V.Subramanian

Hi V. Subramanian
I think your understanding about these OO concepts are relatively
correct. My main reference for OOA/D/P is the book by Booch: Object-
Oriented Analysis, Design and Applications, 2nd edition, 1994. Of
course the 3rd edition just published.
1. Abstraction
According to Booch, Abstraction is first and most important concept in
Object Model. Abstraction means focus on more important charactristics
of a concept and ignore less important ones from the point of viewer.
Abstraction focust the outside view of an object/concept. As an
example, consider the vector in standard library. You want to create a
vector with a given size (constructor), enlarge or grow the vector
(push_back), accesing to elements (operator[]) and ... It is less
important how to implements the vector internelly. You may use vector
for years, but you probably don't know how vector is implemented.
2. Encapsulation
Encapsulation is a complementary concept to Abstraction. For effective
use of Abstraction, the implementation of an object should be hidden.
It means encapsulation guarantees abstraction is used only through
operations (or C++ member functions and some related non-member and
friends functions). Consider again the vector. You can use vector
effectivly through its member functions. For using it, you don't have
to know the details of representation (data members) of vector.
Encapsulation focus on the inside view od an object/concept.
3. Information hiding
It is another term for encapsulation. You hide the data or information
from direct manipulation.

For detailed description of these concepts refer to Booch 2nd edition
and The C++ Programming Language 3rd edition by B. Stroustrup.

At last some simple notes:
1. For information hiding you usually put data members under private
and protected access control.
2. Inline has no relation to information hiding.
3. Encapsulation is for code not people.

Regards,
S. Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top