Signup/Sign In
LAST UPDATED: FEBRUARY 1, 2023

Difference Between Encapsulation and Abstraction

We have OOPS, or Object-Oriented Programming System, in programming languages, which is a programming paradigm supported by multiple languages in which we build our system in terms of real-world items utilizing classes and objects. To make code reusable, modular, and extendable, we apply four major OOPS principles: encapsulation, abstraction, polymorphism, and inheritance. We'll concentrate on two ideas in this article: encapsulation and abstraction.

Difference Between Encapsulation and Abstraction

What is the Definition of Encapsulation?

Encapsulation is a technique for encapsulating all of the data in a single unit, as well as the methods or functions that act on it. We link all the internal workings together with the assistance of encapsulation. It's similar to a capsule in which we may store and eat various items.
Let's use an automobile as an example. We have several capabilities in a vehicle, such as a drive(), stop(), setSpeed(number), and so on, that let us control the car while driving. We also have different attributes of a car, such as model, speed, engine, speed limit, and so on, that provide us with various information about the car. All of these functions and qualities are tied together in a single unit known as a vehicle.

Similarly, we encapsulate all of these methods and attributes with code. Member functions and member variables are terms used to describe functions and attributes.
It becomes quite difficult to operate the program if all of the member functions and data members are interconnected throughout the program. If we update one function, there's a good probability that other functions that rely on it may begin to exhibit issues.

What is the Definition of Abstraction?

Abstraction is a technique for hiding complexity and providing a simple user interface. We may conceal underlying complexity and make the system more user-friendly by using abstraction.

When we use brakes, we have no idea how they operate, and when we move the steering wheel, we have no idea how it turns the automobile. We only know what things do in abstraction, not how they do it. All of the complexity is buried, and all that is required to function is a basic user interface.

When we press the vehicle's accelerator, there are a lot of complicated things going on under the hood. There's an engine with a piston that uses gasoline to create energy using the principles of thermodynamics, and that energy is then turned into rotational energy to propel the car forward. Things grow more difficult as we become more precise about specifics, but when we conceive of the accelerator only as an interface for moving the automobile ahead, it becomes more abstract and simple to operate.

Comparison Table Between Abstraction and Encapsulation

Abstraction Encapsulation
  • Abstraction depicts the pieces required to construct a system.
  • The complexity of a system is hidden under encapsulation.
  • In OOP, the abstraction approach is used at the 'design level.'
  • In OOP, the encapsulation approach is used at the 'implementation level.'
  • The focus in this strategy is on "what" should be done.
  • The focus is on "how" a procedure should be carried out.
  • Certain icons may be clicked on in a mobile device's visual user interface. These icons are known to execute certain actions when clicked. Encapsulation includes internal electronics, LEDs, and other components that come together to produce a television.
  • The end user will have no idea about the implementation specifics once the icon on the GUI is clicked. For example, abstraction refers to the on/off, channel, sound setting, and other buttons of a television.
  • Encapsulation brings together data members and member functions into a single entity known as a class.
  • Abstraction is primarily used to hide implementation complexity and provide simple access to consumers.
  • Encapsulation conceals variables or particular implementations in a class that are often modified in order to prevent others from accessing them directly. Getter and setter type methods are used to retrieve these information.
  • Abstraction is used to hide specific variables, implementations, and components to a greater extent, such as class, interface, and so on. Clients might utilize abstract classes/interfaces that only include information about what they do rather than who they are.
  • It is both the technique and the outcome of identifying shared essential qualities among a group of items. Abstraction is a generalization process in which all of the items under consideration may be located in a superset of objects that share some features but are significantly different in other ways.
  • It's the process of combining functions and data and modifying them into a single entity.

You May Also Like:



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.