Abstraction is a process of collecting important information which will create a base for building up a complex system. Encapsulation is a process of making a complex system easier to handle for an end-user, without worrying about its internal complexities.
The basic difference between “abstraction” and “encapsulation” is that abstraction focuses on “identifying the necessary components for building a system” whereas, encapsulation focuses on “hiding the internal complexities of a system”.
Content: Abstraction Vs Encapsulation
|Basis for Comparison||Abstraction||Encapsulation|
|Basic||Shows, what elements are necessary to build a system.||Hides the complexity of a system.|
|Application||During 'design level'.||During 'Implementation level'.|
|Focus||Focus is on "what" should be done||Focus is on "how" it should be done.|
|Achieved||Achieved through encapsulation.||Achieved through making the members of a class as 'private'.|
|Example||The GUI of a mobile phone, it has some icons to click on, which on click perform the specific function.||After the icon is clicked, the end user has no idea regarding its implementation details|
Definition of Abstraction
Abstraction is a mechanism of extracting the essential elements for the creation of a system, without its implementation details. In abstraction, we have to focus only on what is to be done instead of how it should be done. Abstraction is a thought process; it solves the problem at the design level.
Hierarchical classification helps us to manage a complex abstraction. It allows us to break a complex system into some manageable pieces and create a layered semantics. Moreover, it is a “generalization”. Let’s, take an example of a ‘Bike’, it is made of a number of subsystems such as engine, brakes, lights, sound system, seat and much more, which when assembled creates an object ‘bike’.
The ‘brake’ which is a subsystem of a bike can further be classified as front wheel brake and back wheel brake, but in general, we view all subsystem as brakes. So, we have to manage the complex system by hierarchical classification, which helps us to find out what are the essential elements which will be required to create a system.
So, abstraction is nothing but generalizing “what elements are essential for creating a system”, i.e. picking up the important ones only.
Definition of Encapsulation
Encapsulation is an essential feature of object-oriented programming. It is a mechanism that binds data and code together and also keeps them safe from the outside interference. In simple words, encapsulation hides the complexity of a system, as the keyword here is “data hiding”.
It is like a protective wrapper that hides the data and code inside the class, being accessed by the other code which is defined outside the class and the member function/method which are not the members of that class.
When “data” and code that operates on that data, are linked in a class, the objects of the class are created. The access to the components of the object is restricted by a well-defined interface called access specifier. There are three access specifiers in C++ and Java; they are public, private, protected.
The encapsulation is particularly achieved by making the data and code, that operates on the data, as private. The private members of the class are unable to access by the code defined outside the class. It helps in preventing the misuse of secured data whereas it also hides the complexity of the code.
Let’s take an example of a mobile phone. In a mobile phone, you can perform many functions such as clicking a picture, recording a video/audio, sending a message, access the internet and much more. You consider all these as the features of a mobile phone. But, the end user doesn’t need to know the internal complexity of these features, before using these applications, nor you need to change the functionality of any of the feature.
So, in this way, the encapsulation hides the complexity and also prevent the misuse of the data.
Key Differences Between Abstraction and Encapsulation
- Abstraction focuses on elements that are necessary to build a system whereas, the encapsulation focuses on hiding the complexity of the system.
- The abstraction is performed during the design level of a system. On the other hand, encapsulation is performed the system is being implemented.
- Abstractions main motive is, what is to be done to build a system. Encapsulations main motive is, how it should be done to build a system.
- Abstraction is achieved by encapsulation whereas, the encapsulation is achieved by making the elements of the system private.
Abstraction and encapsulation both are the essential features of OOP. A good encapsulation can achieve a good abstraction.
victor richard says
The differences are well exposed and explained. Thank you.
Very clear explaination. Thankyou.
Thank you for the clear points
Thanks for this article