Abstraction and Encapsulation

In this section of OOAD principles, we will discuss a very important comparison between 2 related concepts.

We have already discussed "Encapsulation".

Now let me introduce a new concept called as "Abstraction". Many times people get confused between 'Encapsulation" and "Abstraction".

So before we get into their interrelationships, let's talk about what exactly Abstraction is.

What is "Abstraction"?

Let me make it simple for you. What is the English grammatical meaning of the word "Abstraction"?

Can we say that "Abstraction" means "Hiding" or "Abstraction" leads to "Hiding"

When we say that Abstraction means hiding or Abstraction leads to hiding, think about this. In this world of systems, what can be the various requirements for hiding that you can have?

In this world of systems, there can be two requirements of hiding.

  • One requirement of hiding is that I completely want to hide a system.

Let me give you an example. I want to completely hide Ramesh as a system. And how do I do it?

Very simply, we know as a rule, whenever we want to completely hide a system, we need to put in a "Level of Indirection" in between.

Let's understand that if your requirement is to completely hide a system, all you need to do is introduce a level of indirection in between and you can completely hide a system.

This is one requirement of hiding that you can have which can be achieved by introducing a level of indirection.

What is the second requirement of hiding?

  • The second requirement of hiding could be that I don't want to completely hide a system but I would want to selectively hide certain details of the system.

Can you think of some real life examples of selective hiding?

What do think about encapsulation?

We know that every well designed system should be encapsulated for the end user. Don't you think it is a case of selective hiding where in you want to show the behaviours but you want to hide the internal structure and the implementation of the same.

And how do you enable selective hiding within your system?

What are "Access Modifiers" meant for?

In all programming languages, you have something called as Access Modifiers; Public, Private and Protected.

Selective hiding is achieved by using Access Modifiers. A practical case where in you need selective hiding is in every system. The moment we talk about encapsulation; encapsulation is all about selective hiding.

Hence in any system, there can be 2 types of hiding.

If your need is for completely hide a target system, use a level of indirection to achieve the same.

While if your need is to selectively hide a certain details of the system, then in that case what you can do is you can use Access Modifiers to do the same.

Although we should not compare these two concepts, many times people ask that what is the inter relationship between "Abstraction" and "Encapsulation".

If you ask me as to what is the inter-relationship between the two, then according to me without abstraction you cannot encapsulate. Without hiding or abstraction you cannot encapsulate.

So according to me abstraction is needed, so as to encapsulate a system which can be achieved either by using a level of indirection or you can use access modifiers to achieve the same.

Many times, at least for this particular topic, it has happened that people say that, Hemant, whatever you are saying looks logical, looks good. But what is given in the books is something else.

Firstly let me tell you that I don't believe in comparison. Secondly let me tell you something that's really important.

Let's try to understand what a book is and what a training program is. According to me, a book or a training program is the author's or trainer's perspective of a corresponding subject.

Whenever someone is writing a book, what is he doing?

He is giving you his perspective of that corresponding subject.

Whenever someone is training, what is he doing?

He is sharing his views of a corresponding subject.

I always tell my participants that whatever I am saying, don't take it as black and white.

Do you agree that a trainer and a participant are 2 different objects of class human being? This means that they have the same structure and same behaviour. Can I say that they have similar capabilities?

Similarly the author and the reader are 2 different objects of class human being and they have similar structure and behaviour. That means they have similar capabilities.

Always remember that when you are reading a book or when you are attending a training program, what you are doing is you are basically reading or listening to the author's or the trainer's perspective of that subject which might be complete, which might be incomplete, which might be incorrect.

It's not the question of correct or incorrect. What happens is that it is quite possible that for certain topics my understanding is right but I am not able to articulate by giving the right example. It's quite possible that today I have a particular view about a particular topic. But it's quite possible that tomorrow I might have a completely different point of view.
I always believe that a book is the author's state of mind at a particular point of time. It is a state which is dynamic. It can change.

I always ask my participants never to take what is written in books as black and white. And in this world of systems, there are only two best friends. One is Common sense and the other is the real world. If your common sense decisions work out in the real world, then your decision is right. If it doesn't work in the real life, then your decision is wrong.

You must have realised that all this while, whenever we are discussing something, I have always said that according to me or in my point of view. And whenever I have basically said anything, I have given a technical reason for it. What I am sharing with you is basically the knowledge that I have gained through books, training experience or through my mentors. I am just sharing my point of view of the corresponding topic. And I have no issues accepting that certain things that I have said may not be completely correct. I will be very happy to rectify it.

So what I am saying is that don't compare me with books. And honestly speaking my job is not to convince. My job is to just share the best practices along with the logical reasoning. And it is for you to decide whether the reasoning is right or wrong. Whether it works in real life or not?

For most of the things that I have discussed with you, I have given you a real life parallel so that you can see for yourself that if it's working in real life, my reasoning is right.

Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer