Abstraction Vs Encapsulation In OOPS

Abstraction Vs Encapsulation In OOPS

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 -

  1. Abstraction is a concept (or design time item) while encapsulation is an implementation (or run time item).
  2. 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().

  1. interface IProject {  
  2.     void PrepareDocuments();  
  3.     void DoProgramming();  
  4. }  

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.

  1. class WaterfallProject: IProject {  
  2.     public void PrepareDocuments() {  
  3.         PrepareHLD();  
  4.         PrepareLLD();  
  5.     }  
  6.     private void PrepareHLD() {  
  7.         Console.WriteLine("High Level Design Doc based on requirement.");  
  8.     }  
  9.     private void PrepareLLD() {  
  10.         Console.WriteLine("Low Level Design Doc based on HLD.");  
  11.     }  
  12.     public void DoProgramming() {  
  13.         Console.WriteLine("Program as per DLD.");  
  14.     }  
  15. }  

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.

  1. class AgileProject: IProject {  
  2.     public void PrepareDocuments() {  
  3.         BasicDocuments();  
  4.     }  
  5.     private void BasicDocuments() {  
  6.         Console.WriteLine("Basic document for user story.");  
  7.     }  
  8.     public void DoProgramming() {  
  9.         Console.WriteLine("Code the User Story.");  
  10.     }  
  11. }  

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.


Similar Articles