What's the difference between abstraction and encapsulation?

Abstraction has to do with separating interface from implementation. (We don't care what it is, we care that it works a certain way.)

Encapsulation has to do with disallowing access to or knowledge of internal structures of an implementation. (We don't care or need to see how it works, only that it does.)

Some people do use encapsulation as a synonym for abstraction, which is (IMO) incorrect. It's possible that your interviewer thought this. If that is the case then you were each talking about two different things when you referred to "encapsulation."


It's worth noting that these concepts are represented differently in different programming languages. A few examples:

  • In Java and C#, interfaces (and, to some degree, abstract classes) provide abstraction, while access modifiers provide encapsulation.
  • It's mostly the same deal in C++, except that we don't have interfaces, we only have abstract classes.
  • In JavaScript, duck typing provides abstraction, and closure provides encapsulation. (Naming convention can also provide encapsulation, but this only works if all parties agree to follow it.)

Its Simple!

Take example of television - it is Encapsulation, because:

  1. Television is loaded with different functionalies that i don't know because they are completely hidden.

  2. Hidden things like music, video etc everything bundled in a capsule that what we call a TV

Now, Abstraction is When we know a little about something and which can help us to manipulate something for which we don't know how it works internally.

For eg: A remote-control for TV is abstraction, because

  1. With remote we know that pressing the number keys will change the channels. We are not aware as to what actually happens internally. We can manipulate the hidden thing but we don't know how it is being done internally.

Programmatically, when we can acess the hidden data somehow and know something.. is Abstraction .. And when we know nothing about the internals its Encapsulation.

Without remote we can't change anything on TV we have to see what it shows coz all controls are hidden.


Abstraction

Exposing the Entity instead of the details of the entity.

"Details are there, but we do not consider them. They are not required."

Example 1:

Various calculations: Addition, Multiplication, Subtraction, Division, Square, Sin, Cos, Tan.

We do not show the details of how do we calculate the Sin, Cos or Tan. We just Show Calculator and it's various Methods which will be, and which needs to be used by the user.

Example 2:

Employee has: First Name, Last Name, Middle Name. He can Login(), Logout(), DoWork().

Many processes might be happening for Logging employee In, such as connecting to database, sending Employee ID and Password, receiving reply from Database. Although above details are present, we will hide the details and expose only "Employee".

Encapsulation

Enclosing. Treating multiple characteristics/ functions as one unit instead of individuals. So that outside world will refer to that unit instead of it's details directly.

"Details are there, we consider them, but do not show them, instead we show what you need to see."

Example 1:

Instead of calling it as Addition, Subtraction, Multiplication, Division, Now we will call it as a Calculator.

Example 2:

All characteristics and operations are now referred by the employee, such as "John". John Has name. John Can DoWork(). John can Login().

Hiding

Hiding the implemention from outside world. So that outside world will not see what should not be seen.

"Details are there, we consider them, but we do not show them. You do not need to see them."

Example 1:

Your requirement: Addition, Substraction, Multiplication, Division. You will be able to see it and get the result.

You do not need to know where operands are getting stored. Its not your requirement.

Also, every instruction that I am executing, is also not your requirement.

Example 2:

John Would like to know his percentage of attendance. So GetAttendancePercentage() Will be called.

However, this method needs data saved in database. Hence it will call FetchDataFromDB(). FetchDataFromDB() is NOT required to be visible to outside world.

Hence we will hide it. However, John.GetAttendancePercentage() will be visible to outside world.

Abstraction, encapsulation and hiding complement each others.

Because we create level of abstraction over details, the details are encapsulated. And because they are enclosed, they are hidden.