In this image, we can see encapsulation in capsules, as we don’t know what chemicals and drugs it has inside; while the thermometer shows the example of abstraction as we don’t know the internal mechanism of getting the temperature reading.
This is a basic example of Abstraction vs Encapsulation that we read in books, but it becomes very confusing when you try to search online to explain it from the technical perspective. It is a very popular and equally confusing question that is asked in about 80% of interviews.
How do Online Resources confuse us?
Basically, online we get these two types of differences -
- Abstraction is a concept (or design time item) while encapsulation is an implementation (or run time item).
- Abstraction is showing only necessary functionality while Encapsulation is hiding the details.
All explanations are only around them.
How are they confusing?
Statement # 1 – Since we are having a technical discussion, we need to talk in terms of classes and this statement doesn’t explain much about them. ☹
Statement # 2 – from this definition, the terms Abstraction and Encapsulation become more confusing and seem to be used interchangeably. These two words, not showing and hiding, look synonyms to each other. ☹
Let us clear our concepts.
To have a better understanding, let us keep the topic Abstraction vs Encapsulation aside for the instance and focus on this small scenario.
Functional Requirement
We need to show the steps done during any type of project.
Technical Implementation
First, I will create an interface IProject, with two methods - PrepareDocuments() and DoProramming().
- interface IProject {
- void PrepareDocuments();
- void DoProgramming();
- }
Now, I have one class, WaterfallProject, implementing the same interface IProject. In its PrepareDocuments() method (being waterfall model project), it will have extensive documentation as High-Level Design and Low-Level Design Document. These two tasks will be done in two private methods - PrepareHLD() and PrepareLLD().
The DoProgramming() method will have to adhere to low-level design and here is its implementation.
- class WaterfallProject: IProject {
- public void PrepareDocuments() {
- PrepareHLD();
- PrepareLLD();
- }
- private void PrepareHLD() {
- Console.WriteLine("High Level Design Doc based on requirement.");
- }
- private void PrepareLLD() {
- Console.WriteLine("Low Level Design Doc based on HLD.");
- }
- public void DoProgramming() {
- Console.WriteLine("Program as per DLD.");
- }
- }
After that, I have another class named AgileProject implementing the same Interface IProject. This class has the agile specific implementation of PrepareDocuments() and BasicDocuments() like this.
- class AgileProject: IProject {
- public void PrepareDocuments() {
- BasicDocuments();
- }
- private void BasicDocuments() {
- Console.WriteLine("Basic document for user story.");
- }
- public void DoProgramming() {
- Console.WriteLine("Code the User Story.");
- }
- }
Explanation
Let us come back to our subject and talk in the in terms of Abstraction and Encapsulation.
IProject interface has two methods PrepareDocuments() and DoProgramming(). At this point of time, it doesn’t know what type of project it will be used in and how documentation will happen in PrepareDocuments() and what guidelines will be followed in the method DoProgramming().
We can call this abstraction as we are abstracting the tasks. Here we saw the example of Agile and waterfall project and in future, we can have some more types and specific implementation of the methods.
WaterFallProject class has PerpareDocument() method which calls two private methods to get different required documents and has a different implementation of DoProgramming() method.
On the same line, in AgileProject class, PrepareDocument() calls BasicDocument() private method while DoProgramming() has its own way of implementation.
Any consumer to these class will never to know how their documentation is done. Here, we are encapsulating those functionalities, which are not significant for the external consumer.
I hope this article is your ultimate and one-stop search for the abstraction vs encapsulation topic. Please feel free to give feedback to improve.