Signup/Sign In

Difference Between Abstraction and Encapsulation

Introduction

Abstraction is the process of gathering crucial data in order to develop a foundation for a complicated system. Encapsulation is a technique for making a complicated system easy to use for a user without having to worry about the system's fundamental intricacies.

The primary distinction between "abstraction" and "encapsulation" is that abstraction is concerned with "identifying the essential components for developing a system," while encapsulation is concerned with "hiding the underlying complexity of a system."

What is Abstraction?

Abstraction is a method of isolating the fundamental pieces for the design of a system without having to worry about the intricacies of its implementation. In abstraction, we must concentrate only on what has to be done rather than how it should be done. Abstraction is a mental process that solves an issue at the level of design.

We can handle a complicated abstraction with the assistance of hierarchical categorization. It enables us to decompose a complicated system into manageable chunks and construct layered meanings. Furthermore, it's a "generalisation." Take, for example, a 'Bike.' It is made up of a variety of subsystems, such as the engine, brakes, lights, sound system, seat, and so on, that when put together form the object 'bike.'

The 'brake,' which is a bike subsystem, may be further categorised as front wheel brake and rear wheel brake, although we consider all subsystems to be brakes in general. As a result, we must manage the complicated system using hierarchical categorization, which allows us to determine which pieces are necessary to build a system.

As a result, abstraction is nothing more than generalising "what parts are required for developing a system," i.e. just focusing on the most significant ones.

What is Encapsulation?

Object-oriented programming relies heavily on encapsulation. It's a method that keeps data and code secure from outside intervention while still binding them together. Encapsulation, in basic terms, conceals a system's complexity, since the buzzword here is "data hiding."

It's like a protective wrapper that keeps the data and code within the class hidden from other code specified outside the class and member functions/methods that aren't members of the class.

The objects of a class are produced when "data" and code that acts on that data are coupled in a class. A well-defined interface called access specifier restricts access to the object's components. In C++ and Java, there are three types of access specifiers: public, private, and protected.

Encapsulation is done in part by keeping the data and the programmes that interact it privately code declared outside the class is unable to access the class's secret members. It aids in the prevention of data abuse while also concealing the code's complexity.

Let's use a mobile phone as an example. A mobile phone may do a variety of tasks, including taking pictures, recording video/audio, sending messages, and accessing the internet, among others. All of these are considered mobile phone functions by you. However, before utilising these apps, the end user does not need to understand the underlying complexity of these features, nor do you need to modify the operation of any of the features.

As a result, the encapsulation conceals the complexity while simultaneously preventing data abuse.

Comparison Table Between Abstraction and Encapsulation

Abstraction Encapsulation
  • Abstraction provides a solution to the problems and challenges that develop throughout the design process.
  • Encapsulation resolves issues and problems that develop throughout the implementation process.
  • Abstraction enables you to concentrate on the object's function rather than how it does it.
  • To protect data from the outside world, encapsulation allows you to combine code and data into a single unit.
  • Interface and Abstract Class are two ways to employ abstraction.
  • Access Modifiers may be used to provide encapsulation (Public, Protected & Private.)
  • Concentrate primarily on what has to be done.
  • Rather of focusing on what should be done, concentrate on how it should be done.
  • During the stage of design.
  • During the stage of implementation.

Conclusion

The key elements of OOP are abstraction and encapsulation. A decent abstraction may be achieved with proper encapsulation.



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.