I have seen many developer/Architect use the term interchangeably and has the reason for it but yes there are differences– a huge difference in terms of hiding information. I try to explain it in a Simple way.
Let’s start by the definition.
Encapsulation: binds data and behaviors together in a Single unit and behaviors acts on the data.
Abstraction: Hiding the implementation details and expose the functionality to the world.
According to both definition, both try to hide data from rest of the world and expose some behaviors/method so other System/API can use it.
At this point Both are same so If someone uses those term Interchangeably they are correct.
But hold why then Two fancy concepts are side by side in OOP why they are not merged into a one and called it “Information hiding”
To understand the same take a look Suppose you have to implement a car.
So when you rotate the steering lots of thing happening inside and eventually car is moving to the direction you rotate the steering.
Now let explain the Action in details
Input: Steering rotation direction.
Output: car moves in the Direction steering rotated.
but what is happening inside is a black box to the user—That is called Abstraction
So technically it says What to abstract from User?
Abstraction is a functionality which helps the developer to identify what is the functionality that should be abstracted and exposed as a function/method which takes User input and returns desired result what User wants.
In a car Steering functionality, Braking functionality, Auto parking –these are the functionalities has to be abstracted from User— User less interested How it works but what they interested is What should I do(Input) and What will be the Outcome. So according to me Abstraction is
Abstraction: By Abstraction, developers identify the functions what should be published in API and what input it takes and What Output it returns.
So, another point of view is, Abstraction helps us to the generalization of a functionality– So When you design a function’s input or output you should be very careful about the data type you used– It should be supported all possible combination on which function can be applied.
Now come to Encapsulation It tells about How to achieve the functionality— which has been identified by Abstraction.
So it tells us about the packaging the data and behaviors.
Take the same example use steering to move the car.
Encapsulation: identifies the different parts associate to move the car using user instruction like steering, Wheel, Engine.Petrol . Also, it identifies the algorithm/behaviors which will be applied to these data(wheel, steering, engine, petrol) to move the car, and help to binds or packaging as one single unit. In my perspective Encapsulation definition is.
Encapsulation:Encapsulation Helps to understand what are the data and functions, that should be bundled as a Single Unit so User can act on them without knowing internal details and get the job done.
Explanation of the figure: When you design an API/Class always there is two perspective one is Developers View and one is API User view. From Developers View Abstraction is to identify the features to be provided and Encapsulation is the process to communicate with internals things and provide the functionality. So it makes sense to have two distinct terminology Abstraction and Encapsulation.
But for User of the API/Class, It is like what functionality is exposed and what is the input and what will be the output so functionality an API provides nothing but Opaque things to them they provide input and got Output –API or Class is a barrier or Facade for them So for them it is just an Information hiding so Abstraction and Encapsulation has no meaning for them. It can be used alternatively to mention information hiding.
Conclusion : Abstraction and Encapsulation both are used for hiding information context but their purpose is different. Abstraction helps to understand the functionality User interested for and providing the same to the user as a black box. Encapsulation is about the gathers the required data and algorithm to solve the purpose for the user and tied them in a single Unit so the user of the API doesn’t have to collects the data and apply the algorithm by itself to get the job done.
Is Data Abstraction and Encapsulation a fancy term of Information Hiding?