Abstraction and Encapsulation are major concepts of Object-Oriented Programming. The term “Abstraction” associates with the method of hiding unwanted data and “Encapsulation” associates with covering up data under a single unit.
In Abstraction, the focus lies on the functioning of the object. In Encapsulation, internal details and mechanics of object function are hidden.
Abstraction vs Encapsulation
The difference between abstraction and encapsulation is that abstraction is about gaining relevant and selective information from a larger data pool while encapsulation is the method of holding up data as a single entity and hiding it. The objects that conduct abstraction are encapsulated but encapsulating objects do not need to be abstracted.
Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!
The Abstraction function hides unnecessary information from the user. It selects data from a larger pool of information.
This function enables the selection and display of only relevant data to the object. It helps us decide about which information needs to be visible and what needs to be hidden from the user.
The Encapsulation function processes the data and the code operating on it into a single unit. It can be assumed as a protective cover that stops random access of code.
It makes a complex system easier for users to understand and work on without worrying about the internal details and mechanics of the system.
|Parameters of Comparison||Abstraction||Encapsulation|
|Functioning||It is the process of gaining selective information from a larger data pool.||It is the method of holding up data as a single entity.|
|Level of implementation||The problems get solved at the designing level or while the user is using the interface.||The problems get solved when the code runs that is during the final level.|
|Ways of implementation||Abstract classes and interfaces method is used to implement abstraction.||It is implemented by using an access modifier by granting either private or public access.|
|Methods used to hide data||Abstract classes and interfaces are used to hide implementation hurdles.||The method of getters and setters is used to hide data.|
|Relation between the functions||The objects that help in carrying abstraction forward are encapsulated.||The objects performing encapsulation do not need to be abstracted.|
What is Abstraction?
In data abstraction, only essential details are visible to the user. The method of abstract classes and interfaces is used to implement abstraction.
It selects data from a larger pool and then creates classes of similar types. Classes are created by encapsulation when similar information in form of the same data type and functions is grouped.
The class has the freedom to decide which data function gets displayed to the user and which can not.
Access specifiers are used for implementing abstraction and enforce restrictions of objects. Abstraction helps the user to create high-level codes, restricts code duplication, and extends the reusable criterion of the information.
The relevant and selective showcasing of data to the public ensures the security of the application or program. The program functions can be altered and updated from our side without affecting the end-user.
Abstraction enables high-level programming and machine representation.
The problems are reviewed and solved at the planning (designing) level and during the user interface interaction. In order to hide implementation hurdles, methods of abstract classes and interfaces are used.
The objects in code that are to be abstracted further are encapsulated. Abstraction focuses on knowing the function of the code and not on the logic applied.
What is Encapsulation?
The processing of data and the code operating on it into a single unit is Encapsulation. The single unit contains data performing the same functions and hence is called Class.
Using encapsulation, programs or applications are much easier to use and understand. The internal details and mechanics of the system need not be known to the user to operate on the code.
The access modifier is used in the system which decides to grant data access to the public or to a limited group of people for private use.
In encapsulation, the data is enclosed and protected from the world like a capsule, so that it does not get manipulated.
It can be said that encapsulation is a kind of abstraction but it is more security-specific about data hiding. It gets implemented during the final level and by the method of getters and setters.
This method allows others to read and modify the value of private members only if they have been granted access.
The data members are labeled as private, public, or protected according to the range of access granted. By default, all data members and functions are made private by the compiler but they can be changed using an access modifier.
Main Differences Between Abstraction and Encapsulation
- Abstraction is the process of gaining relevant and selective information from a larger data pool. Encapsulation is the method of holding up data as a single entity.
- In abstraction, the problems get solved at the designing level or during the user interface. In encapsulation, the problems get solved when the code runs that is during the final level.
- The method of abstract classes and interfaces is used to implement abstraction. Encapsulation is implemented by using an access modifier by granting either private or public access.
- Abstract classes and interfaces are used to hide implementation hurdles in abstraction whereas encapsulation uses the method of getters and setters is used to hide data.
- The objects that conduct abstraction are encapsulated but encapsulating objects do not need to be abstracted.
I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️
Sandeep Bhandari holds a Bachelor of Engineering in Computers from Thapar University (2006). He has 20 years of experience in the technology field. He has a keen interest in various technical fields, including database systems, computer networks, and programming. You can read more about him on his bio page.