Hello.

Well, im a little confused as to what exactly the terms Data Abstraction and Data Encapsulation mean:

This is what i read:

Data Abstraction:data abstraction is a process of representing the essential
features without including implementation details.

Data Encapsulation:: Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user.

Can anyone please give me an example to exactly differentiate between the two?

Thanks.

Recommended Answers

All 6 Replies

Data Abstraction means something like this :

//Shape provides no implementation of the draw functions
//It abstracts the draw function from other shapes meaning that it
//defines functions that all type of shapes should necessarily have.
class Shape{
  public: virtual void draw()const=0;
};
class Ellipse : Shape{
 public: void draw()const{ API.drawEllipse(); }
};

Data Encapsulation :

class Info{
 string name;
 int uniqueId;
//....
public:
 string getName(){ return name; }
 int getId(){ return uniqueId; }
};

Notice that we do NOT provide public access for the variables name and uniqueId;
Instead we hide them or encapsulate them, and rather make a function that returns
a copy of them.

Thanks for the reply.

Can you please tell me if my understanding is correct:

Data Abstraction: we hide away the implementation details ie we do NOT show how a particular function is implemented.

Data Encapsulation:we hide data from other classes.

i have one more doublt:

class Sample {
     private:
             void doNothing();
};

Now, in the above example, we are hiding doNothing() method. What is this called as?
Is it still called as Data Encapsulation? (But we are not hiding data here)

>>Data Abstraction: we hide away the implementation details ie we do NOT show how a particular function is implemented

Thats exactly what data encapsulation is.

Data Abstraction means that we factor out common functions from a class and make a separate class thats abstract, so that the other classes can inherit form it.

Ok i got it now. Thanks!

Exactly in encapsulation access is restricted for the hidden data.Where as in abstraction no such rules exists......clearly abstraction is a process of hiding unnecessary implementation information from user.where as encapsulation is a process of hiding private implementation details from the public interface.

Will u pls tell me polymorphism nd inheritance??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.