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.


Difference between Abstraction and Encapsulation :-

Abstraction

  1. Abstraction solves the problem in the design level.
  2. Abstraction is used for hiding the unwanted data and giving relevant data.
  3. Abstraction lets you focus on what the object does instead of how it does it.
  4. Abstraction- Outer layout, used in terms of design. For Example:- Outer Look of a Mobile Phone, like it has a display screen and keypad buttons to dial a number.

Encapsulation

  1. Encapsulation solves the problem in the implementation level.
  2. Encapsulation means hiding the code and data into a single unit to protect the data from outside world.
  3. Encapsulation means hiding the internal details or mechanics of how an object does something.
  4. Encapsulation- Inner layout, used in terms of implementation. For Example:- Inner Implementation detail of a Mobile Phone, how keypad button and Display Screen are connect with each other using circuits.

Encapsulation

Encapsulation from what you have learnt googling around, is a concept of combining the related data and operations in a single capsule or what we could say a class in OOP, such that no other program can modify the data it holds or method implementation it has, at a particular instance of time. Only the getter and setter methods can provide access to the instance variables.

Our code might be used by others and future up-gradations or bug fixes are liable. Encapsulation is something that makes sure that whatever code changes we do in our code doesn't break the code of others who are using it.

Encapsulation adds up to the maintainability, flexibility and extensibility of the code.

Encapsulation helps hide the implementation behind an interface.

Abstraction

Abstraction is the process of actually hiding the implementation behind an interface. So we are just aware of the actual behavior but not how exactly the think works out internally. The most common example could the scenario where put a key inside the lock and easily unlock it. So the interface here is the keyhole, while we are not aware of how the levers inside the lock co-ordinate among themselves to get the lock unlocked.

To be more clear, abstraction can be explained as the capability to use the same interface for different objects. Different implementations of the same interface can exist, while the details of every implementation are hidden by encapsulation.

Finally, the statement to answer all the confusions until now - The part that is hidden relates to encapsulation while the part that is exposed relates to abstraction.

Read more on this here