What is the difference between encapsulation and abstraction?

Encapsulation and abstraction are two of the four pillars of object-oriented programming (OOP). Let’s discuss each of them one-by-one.

Encapsulation

Encapsulation packages several items, such as variables and functions, into a single entity.

Encapsulation is a technique that makes the system easier to handle and use for the end-users since it removes the internal details and complexities from the user’s view. Encapsulation also ensures better control of your data because we can change one part of the code without affecting the other parts.

Encapsulation can be achieved by using classes in our system. We can bind the related functions and data into a single class that an object can access.

Let’s look at the following code.

#include <iostream>
using namespace std;
class Car{
private:
float mileage;
public:
//function to set the value of mileage
void set(float x){
mileage = x;
}
//function to get the value of mileage
float get(){
return mileage;
}
};
int main() {
//create a Car object
Car obj;
//setting the mileage
obj.set(10.5);
//getting the mileage
cout << obj.get() << endl;
return 0;
}

Now, you can see that the user can set and get the car’s mileage without knowing the inner implementation details of the car class. This is an example of encapsulation.

Abstraction

Abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. Abstraction can be achieved by using access specifiers in a class.

The following are some suggestions for implementing abstraction in a class.

  • Make all member variables private.
  • Helper functions should be private.
  • Basic functions that the user needs should be public.

Let’s look at the following code.

#include <iostream>
using namespace std;
class Car{
private:
float mileage;
int fuel_tank;
public:
Car(float x){
mileage = x;
fuel_tank = 45;
}
void range(){
cout << "The Range of the Car is : ";
cout << fuel_tank * mileage;
cout << " km.";
}
};
int main() {
//create a Car object
Car obj(10.5);
obj.range();
return 0;
}

As you can see, the mileage of the car isn’t accessible in the main function. Additionally, the user doesn’t know the implementation details of the range. He calls that function, and only necessary information is shown to him. This is an example of abstraction.

The following is a summary of the difference between abstraction and encapsulation.

Summary of key differences

Encapsulation

  • Process to store related information.

  • Can be achieved by using classes.

  • The object that results in encapsulation does not need to be abstracted.

  • Solves problems at the implementation level.

Abstraction

  • Process of hiding unwanted information and showing related information.

  • Can be achieved by using access specifiers, abstract classes, and interfaces.

  • The object that helps to perform abstraction needs to be encapsulated.

  • Solves problems at the design level.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved