Hi

Can anyone explain what the meaning of encapsulation and abstraction in OOPs?

Encapsulation can have different meanings depending on whom you ask. Most of the time you'll see encapsulation rolled up along with data hiding, which while appropriate, is technically incorrect. My interpretation of encapsulation is strictly the bundling of data and behavior. In the case of C++, those would be the data members and member functions of a class, respectively.

If you collect the data and behavior into a single entity (such as an object of a class), that's encapsulation.

Abstraction is the concept of stripping away unnecessary details until you have the core of what an entity is intended to do. For example, if you have a dictionary, you don't care how the dictionary is stored as long as it does what you need. So the underlying data structure would be abstracted away by a public interface.

Let's say you're writing such a dictionary class. The important operations would be adding a record, removing a record, and retrieving a record. The user of the class doesn't care that you chose a binary search tree to do the heavy lifting of those operations, so you hide it behind the public interface:

class Dictionary {
    Tree<Key, Record> _store;
public:
    void Add(Record rec) { _store.Insert(rec.Key, rec); }
    void Remove(Key searchKey) { _store.Remove(searchKey); }
    Record Retrieve(Key searchKey) { _store.Find(searchKey); }
};

This is an abstraction. You're taking the Tree class and wrapping a Dictionary interface around it such that the user of the Dictionary doesn't need to know about the Tree class. The Dictionary class is at a higher abstraction because it represents what you want to do rather than how you're actually doing it.

>>Most of the time you'll see encapsulation rolled up along with data hiding, which while appropriate, is technically incorrect

care to expand?

>>Most of the time you'll see encapsulation rolled up along with data hiding, which while appropriate, is technically incorrect

care to expand?

People tend to think that encapsulation includes data hiding, when that's not the case. As for the "appropriate" remark, I was referring to the meaning of encapsulation as a joke. ;)

>>care to expand?

I tend to think of data hiding as a likely consequence of encapsulation. In a similar sense that encapsulation is the bundling of data and behaviour in a single entity, I usually also attach to that the presence of some form of hiding the details (like the stuff that is done under-the-hood to preserve the class invariants). This is different from abstraction, in my book.

Abstraction is to define, like in the example given, what a dictionary object should do. While encapsulation is to bundle all the things necessary to get that done.

Now, it doesn't mean that you have to hide the information that is bundled in your class, in fact it doesn't require that you hide anything. But, in general, you will expose whatever is necessary for the object to fulfill its abstract purpose and its concrete purpose, and hide everything else. So, information hiding is usually a part of the process of encapsulation, but not strictly necessary (although highly recommended, of course).

Also, may I remind you that information hiding takes two forms, hiding data and hiding functionality (or behaviour). Sometimes you do one, sometimes the other, and sometimes both, depending on the problem.

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.