Encapsulation And Abstraction, Same Or Different?

When I first began to learn object oriented programming, I learned that there are three tenets of OOP – Encapsulation, Inheritance and Polymorphism. However, I have also seen other authors who opine that there is actually a fourth – Abstraction. One question now comes to mind: Is encapsulation the same as abstraction?

I decided to dig a little deeper, and I have come to realize that there’s a subtle difference between the two concepts, and this is what I have briefly presented in this article.

Wikipedia defines encapsulation thus: “Encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components.”

Encapsulation helps us hide the actual state of an object from the outside world**. The outside world is only able to access public methods which modify the state of the object. Take a look at the example below, for instance.

class Program  
{  
    //Demonstrating Encapsulation  
  
    static void Main(string[] args)  
    {  
        Dog dog = new Dog();  
        dog.Feed();  
        dog.Abandon(2);  
    }  
}  
  
class Dog  
{  
    public Dog()  
    {  
        hungerLevel = 10;  
    }  
      
    public void Feed()  
    {  
        hungerLevel--;  
    }  
  
    public void Abandon(int numberOfDays)  
    {  
        hungerLevel += numberOfDays*10;  
    }  
      
    private int hungerLevel;  
}
class Program  
{  
    // Demonstrating Abstraction  
  
    static void Main(string[] args)  
    {  
        Dog dog = new Dog();  
        Daughter.FeedDog(dog);  
    }  
}  
  
class Daughter  
{  
    static public void FeedDog(Dog dog)  
    {  
        GoToMarket();  
        BuyDogFood();  
        dog.Feed();  
    }  
  
    private static void GoToMarket()  
    {  
        //Logic here  
    }  
  
    private static void BuyDogFood()  
    {  
        //Logic here  
    }
  
}

The dog object has a private field – hungerLevel, and two public methods – Feed and Abandon. Ideally, we cannot access the hunger level of a dog directly. We can only alter the hunger level by feeding it, or by abandoning it for a few days.

In C#, when we access the public properties of a class, we are actually accessing getters and setters which are special methods. The getters and setters now modify the private fields. So in the same way, the private fields (defining the object's state) cannot be directly modified by the outside world.

Abstraction on the other hand refers to hiding complex implementations. Think of your phone for instance. You only have a few buttons you can press to perform certain actions. How these actions are performed, you don't really care. How the pressing of a button sends signals that are converted to bits and bytes that the computer can understand, you don't need to know. All you want is functionality. Similarly, we can hide properties and methods from the outside world (by making them private to just the class), and then we expose only certain methods that "encapsulate" all that complexity for the client to access.

The code snippet above shows an example of abstraction. The dog owner instructs his Daughter to feed the dog. The Daughter knows she has to go to market and buy dog food, but all the dog owner needs is that the dog should be fed. Other activities are simply “abstracted” beneath the FeedDog method.

So we can conclusively say that abstraction can be seen in a certain sense as an extension of the concept of encapsulation but from a broader perspective.

Thank you for reading. Please feel free to let me know your thoughts in the Comments section. Also check out my previous articles on Specification Pattern in C# and Composite Specifications.

**The state of an object is defined by the value of all its private fields at every instance.


Similar Articles