Difference Between Encapsulation and Abstraction

Learn via video courses
Topics Covered

Introduction

Have you ever observed what makes it easy and enjoyable to drive a car? There are various things in a car like the steering wheel, breaks, accelerator, clutch and the complex infotainment system. Everything is tightly coupled with each other and provides a simple user interface. All the complexities or parts are hidden from the user which makes driving more fun, easy and enjoyable.

Similarly, in programming languages, we have OOPS i.e. Object-Oriented Programming System, a programming paradigm supported by various languages where we design our system in terms of real-world entities using classes and objects. Here we use 4 main principles of OOPS which are encapsulation, abstraction, polymorphism and inheritance to make code reusable, modular and extensible. In this article we are mainly going to focus on 2 principles i.e. Encapsulation and Abstraction.

Let's first try to understand about encapsulation -

What is Encapsulation?

Encapsulation is a technique to bind all the data in a single unit along with methodds or functions that operate on that data. With the help of encapsulation we bind all the internal workings together. Its just like capsule in which we can store multiple things and consume them.

What is Encapsulation

Let's take an example of a car. In a car we have so many functionalities like drive(), stop(), setSpeed(number) etc. which helps us to control the car while driving, apart from this we have various properties of a car like model, speed, engine, speedLimit etc. which gives us the various information about the car. All these functions and properties are bound in a single unit called a car.

Similarly, in code, we wrap all these functions and properties. Functions are known as member functions and properties are known as member variables.

Member Functions in Encapsulation

See in the above figure we have a class named as car and we have all the member functions and member variables wrapped inside this single unit and this binding of data is known as encapsulation.

Why do we Need Encapsulation?

If we have all the member functions and data members of the program interdependent throughout the program it becomes very difficult to use. If we make changes in one function then there are high chances that other functions which depend on it will start to show the bugs.

Need for Encapsulation

See the above image where functions and data members are placed anywhere in the code which are dependent on each other. This style of coding makes it very complex to use and make changes.

But if we wrapped data members and related member functions together then it becomes easy to access and modify them. It reduces the complexity of code and all the related things come in a single unit.

Importance of Encapsulation

Encapsulation also helps us to hide the data and gives power to the programmer to define the access of data. Some data is only available to use in the class, some data is accessible by the public. That's why encapsulation is very important to make things easy.

What is Abstraction?

Abstraction is a way to hide complexities and give a simple user interface to the user. With the help of abstraction, we hide internal complexities and make the system more user-friendly.

In a car, we have so many things to understand the abstraction, when we use brakes we don't know how it works, when we rotate the steering wheel we don't know how it turns the car. In abstraction, we just know what things do rather than how they do. All the complexities are hidden and only a simple user interface is provided to work.

When we use the accelerator of the car there are so many complex things happening under the hood, there is an engine that has a piston that takes fuel to generate energy with the laws of thermodynamics and this energy is converted into rotational energy to move it forward. As we go more specific about details things start to become more complex, but as we start to think about the accelerator only as an interface to move the car forward it is more abstract and easy to use.

What is Abstraction

Why do we Need Abstraction?

Let's imagine the scenario where we are using brakes to stop or reduce the speed of a car. If we require to know how it works and if we are exposed to its internal working it becomes very difficult to use it. If this complex system of breaks is exposed to someone else he can make changes in it and perform malpractices. So to overcome this issue abstraction is used, abstraction provides both securities as well as it gives a simple user interface by hiding internal complexities.

In coding, consider the following image: only a few functions are accessible and others are hidden from the end-user of the program. This way we can hide complexities and internal working of code and hide it from others to secure core functionalities of the program.

Need for Abstraction

Encapsulation vs Abstraction

The difference between encapsulation and abstraction is as follows.

EncapsulationAbstraction
In encapsulation, we focus on grouping the properties and methods of the object together inside a single unit.In abstraction, we focus on hiding the complex methods and only showing the essential things to the user.
It makes code more modular and easy to understandIt makes applications easy to use by hiding underlying complex working.
We provide security to the properties and methods by deciding who can access them.We provide security to the application by hiding the working part from the user.
Encapsulation is about hiding the information.Abstraction is about hiding the implementation. or internal working.
It controls who can see and use an object’s data and methods by setting rules (public, private, etc.).It focuses on defining what an object does, not how it does it.
Encapsulation is like wrapping an object with rules to protect its data.Abstraction is like describing what something can do without revealing how it does it.
Encapsulation secures the inside of an object from unauthorized access.Abstraction defines a clear “what” without exposing the “how.”

Advantages of Encapsulation

  1. It makes code modular and binds all properties and behaviors of the same entity in a single unit.
  2. Because all similar things are wrapped in a single unit it becomes easy to do maintenance.
  3. Code becomes more modular because of encapsulation.
  4. Coders can specify the access of data and decide who can access it.
  5. We can validate the data before assigning it to the variable. Fuel in the car can never be negative.

Advantages of Abstraction

  1. Abstraction helps to hide complexities from the user to give a simple user interface.
  2. It improves security just by showing essential details.
  3. Due to abstraction, the user only focuses on what the object does instead of how it does.
  4. Because of abstraction we can update the internal implementation by keeping the same interface so users will not have to adjust. For example, disk brakes and drum brakes have different internal working but both of them are accessed with a similar interface.

Conclusion

In this article, we saw the difference between Encapsulation and Abstraction

  • Encapsulation organizes data and functions within a single unit, promoting modularity and easier maintenance.
  • It enhances security by controlling data access through access modifiers.
  • Abstraction simplifies user interactions by hiding complex internal workings.
  • It improves security by exposing only essential information to users.
  • Both encapsulation and abstraction are essential tools for creating secure, user-friendly, and maintainable software systems. They bridge the gap between complexity and usability, ensuring better software design and experiences.

See more