DEV Community

loading...
Cover image for A Refresher on Encapsulation and Abstraction in OOPs

A Refresher on Encapsulation and Abstraction in OOPs

veerreshr
I write all the random stuff I find interesting.
Updated on ・3 min read

Computer Science students, programmers and newbies to Object Oriented Programming(OOPs) are always confused with two terms : Encapsulation and Abstraction. Usually when someone says that encapsulation is a type of abstraction, it creates more confusion. Coming across terms like Encapsulation and Abstraction in Object Oriented Programming is very stressful to those who are just starting out. The jargon that is used for the concepts of Encapsulation and Abstraction is confusing because it does not mean the same thing to any given person. Today we are going to understand these jargons in OOPs.

Let us first look at their definitions,

Abstraction is the method of hiding the unwanted information.

Encapsulation involves combining data and functions in a single unit (called as a class) and restricts access to data by any object of the unit. This functionality caused by data restriction makes encapsulation also called data hiding.

For a common man, it sounds like the same thing as hiding data. For a programmer its different as implementation and information hiding. Let's consider a typical example to figure this out. Let's say we use an Android phone, each mobile contains screen, camera, battery, etc., but the implementation of this functionality of phones can be different. Like one phone will be having an 108 MP camera and the other will be having a 12 MP camera. Here, the end user only knows about the cameras (abstract method or function) and not the way they are implemented internally for each device. This is referred to as abstraction. In the abstraction we know the high level knowledge about system.
Now about Encapsulation, consider a situation where you have 2 departments for your organization (sales, product). At some point and for some reason the product team wants to know about the sales of a product. Here the product team can't access the data of sales directly (as its restricted only for sales team access). First the product team needs to contact some sales officer and request him to get data they needs. This is what encapsulation meant, here sales team comprises of data along with some officers(methods) who help other team people access data by getting in contact with them. So sales team data is only accessible by sales team persons, if some one apart from sales team wants to get the data, they contact sales officer (function or method) of sales team(class) and they can't access directly without help of sales team officer.

In short,
These two things differ from each other from the perspective you see:

  • Abstraction is providing generalization (all the features are same for all devices, but their implementation is different) and hiding their implementation.
  • Encapsulation is hiding of internal data from outsiders (hiding of all internal functionality of the code or APK).
  • Abstarction is Implementation hiding.
  • Encapsulation is Information hiding.
  • Abstraction can be viewed as high level view of logic.
  • Encapsulation also implements abstraction, as it restricts the data from others.

Still confused ? Lets understand with code

class maths
{
private:
  int a, b;
public:
  maths (int x = 0, int y = 0)
  {
    a = x;
    b = y;
  }
  int getA(){
   return a;
  }
  int getB(){
   return b;
  }
  void set(int x, int y){
    a = x;
    b = y;
  }
  int add ()
  {
    return a + b;
  }
};
Enter fullscreen mode Exit fullscreen mode

Internal representation of any object of maths class is hidden outside the class. That is the variables a and b can not be accessed directly by other class objects. These variables are restricted to only maths class objects, if some other class object wants to access the variables a,b they need to contact some maths class object and get data through them. This restriction of data is called Encapsulation.

Variables a and b are made private so that these can only be manipulated by set method, which is only accessible to objects of maths class. This binding of data and methods together is also called as encapsulation.

Now lets talk about abstraction,

int main ()
{
  maths math_object = new maths(1, 2);
  int sum = math_object.add(); //add function is abstracted
  cout << sum;
}
Enter fullscreen mode Exit fullscreen mode

Here, the implementation of the add function is known as Abstraction. Because, we know the necessary details (add function) that we need and we don't know the unnecessary detail (internal working of add function) for performing a particular action (adding two numbers). This masking of the implementation of the addition functionality (unwanted information) is referred to as Abstraction.

Here is a table to represent the differences

Abstraction vs Encapsulation
source of the table : differencebetween.net
If this article helped you somehow, please leave a like your suggestions here ❤.

Discussion (0)