By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,191 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,191 IT Pros & Developers. It's quick & easy.

understanding of data abstraction, information hiding and encapsulation

P: n/a
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

Sep 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2007-09-25 06:42, su**************@yahoo.com, India wrote:
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.

--
Erik Wikström
Sep 25 '07 #2

P: n/a
On Sep 25, 7:42 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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

Sep 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.