Abstraction vs Encapsulation is one of the most confusing concept for new developers. I have seen even experienced developers getting confused over the question, that what is the difference between abstraction and encapsulation? Both these concepts seem related to data hiding and many people use them interchangeably which results in increasing this confusion even more. Let’s try to understand the difference between abstraction and encapsulation in this article.

Abstraction

If you search for Abstraction on Wikipedia, you will come up with this explanation

Abstraction is a technique for managing complexity of computer systems. It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level. The programmer works with an idealized interface (usually well defined) and can add additional levels of functionality that would otherwise be too complex to handle. For example, a programmer writing code that involves numerical operations may not be interested in the way numbers are represented in the underlying hardware (eg. whether they’re 16 bit or 32 bit integers), and where those details have been suppressed it can be said that they were abstracted away, leaving simply numbers with which the programmer can work.

The more you read it, the more you get the impression that abstraction is about hiding data or complexity. I like to define abstraction as

Abstraction is the process of simplifying a complex system, entity or concept

Now simplifying complexity does not necessarily need you to hide anything for example let’s think of colors, there can be millions of shades of color and we needed to come up with a way to simplify how to use colors in computer systems. Imagine if you had to have a name for each shade of colors, who would have gone through that pain? Therefore we came up with an Abstraction of color called RGB (Red Green Blue) Model. RGB is nothing more than an abstraction of colors, it provides you a mechanism of forgetting about the complexity of color names and shades and just focus on integer values between 0-255 for each of the primary colors. Similar color abstraction is CMYK.

If you remember just this one example you will never again get confused with Abstraction, Abstraction simply is the process of simplifying a complex system, entity or concept.

Encapsulation

Now Let’s look at the definition of encapsulation, according to Wikipedia encapsulation is

The packing of data and functions into a single component.
In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  • A language mechanism for restricting access to some of the object’s components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

Again when you read this definition, you will get the feeling that it is all about hiding the data which is partly true. When you ask people for definition of Encapsulation a lot of times they will reply you by saying

It is process of hiding data by using Private, Protected keywords.

Again this definition is partly true, the real problem that encapsulation is trying to solve is

Only show what is necessary

Think of your wrist watch for a moment, your watch has hands that show you current time but to change the time you cannot touch them directly. Instead there is a pin on the side that you can rotate to change the time. You also cannot see internally how your watch is functioning, because you do not need to. As a user of wrist watch all you want to know is what time it is right now and you want to have a way of changing that time if you want. This is Encapsulation!

So to sum it up Encapsulation means to hide the details and show only what the end user is interested in the system.

Summary

Abstraction Vs Encapsulation can be confusing and it is my favorite topic during technical interviews to confuse people, I have seen most of time it works 🙂 Understanding this concept will help you a lot during your development talks and you can be clearer about which one means what when someone uses it incorrectly.