Abstraction vs Encapsulation in OOPS

Atul Sharma

Download source code from here

In this image, we can see Encapsulation in Capsules as we don’t know what chemicals and drugs it has while 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, but it becomes very confusing when you try to search them online and explain in the technical perspective. It is a very popular and equally confusing question asked in about 80% of interviews.

How do Online Resources confuse us?

Basically, online we get these two types of differences –

See also  C# String Interning for Efficient String Comparison

Statement # 1 – Abstraction is a concept (or design time item) while encapsulation is an implementation (or run time item).

Statement # 2 – Abstraction is showing only necessary functionality while Encapsulation is hiding the details of the particular.

All the explanations are only around them.

How are they confusing?

In statement # 1 – Since we are having technical discussion, we need to talk in terms of classes and this statement doesn’t explain much about them.

In statement # 2 – from this definition, the terms Abstraction and Encapsulation become more confusing and seem to be used interchangeably. These two words,
“showing only necessary” and “hiding the details”, are synonyms to each other.

Let us clear our Concepts

To have better understanding let us keep the topic of Abstraction vs Encapsulation aside for the moment and focus on this small scenario.

Functional Requirement – We are needed to show the steps done during any 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().  

DoProgramming() method will have to adhere to low level design and here is it’s 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 of Abstraction and Encapsulation.

See also  Cloud Computing: Deploy an ASP.NET Core App to PCF?

Class Structure

IProject interface has two methods PrepareDocuments() and DoProgramming(). At this point of time it doesn’t know 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, we have no idea what will they do and how they are going be implemented in future.

In AgileProject class, PrepareDocument() calls BasicDocument() private method while DoProgramming() has its own way of implementation.

On the same line, WaterFallProject class has PerpareDocument() method which calls two private methods to get different documents and has different implementation of DoProgramming() method.

Consumer Class

class Program
    {
        static void Main(string[] args)
        {
            ExecuteProject(new WaterfallProject());
            ExecuteProject(new AgileProject());   
            
        }
        static void ExecuteProject(IProject project)
        {
            project.PrepareDocuments();
            project.DoProgramming();
        }
    }

As we can see, Our consumer class Class has one method ExecuteProject(IProject project). At this points, this method doesn’t know what type of project it will be executing as it just expects IProject. That is again abstraction.

In Actual class implementation, we have few methods to get the work done. That may, in turn, call another method to have specific execution. So hiding of internal working of waterfall and agile project documentation and programming showcase the concept id encapsulation.

See also  All About Method Overloading in C#

I hope this article is your ultimate and one-stop search for the abstraction vs encapsulation topic. Please feel free to give feedback and download source code from here