Difference Encapsulation Abstraction
Summary: It is very confusing concept, as one depends on other, for a Java beginner to understand. Explained in simple terms in this tutorial "Difference Encapsulation Abstraction".
How to explain the difference between Encapsulation and Abstraction before an Interviewer?
Encapsulation is an OOPS concept. As everyone says, Encapsulation is binding the data with the code that manipulates it. To put more clear, in encapsulation, the object and instance variables in the class are binded together so that other objects cannot access the data. That is, one object’s data is safe from external interferences. It can say like this also: the object’s data is abstracted (hidden) from other objecs. Encapsulation results in abstraction. To achieve abstraction, encapsulation is one way. Abstraction means hiding something.
Know more about encapsulation at Java Encapsulation, Abstraction, Data hiding and Data binding.
Abstraction is the process of hiding the implementation details and showing only functionality to the user. For example, you are stopping the car by pressing the brake. The car stops. Are you aware of the type (pneumatic and hydraulic) of the brake and how the brake works? Without knowing the brake mechanism, you are using the brake. That is, the brake mechanism is hidden (abstracted) from you. This is called abstraction. Similarly, a house-wife uses a TV without knowing electronics or how TV is made and works. Now I am sure, that you understood what is abstraction. One way is achieving abstraction in a computer language is encapsulation. Both abstraction and encapsulation are very different. In the interrview, give the definiton of encapsulaiton and give Car and TV examples for abstraction.
Java language comes with abstract classes and interfaces to achieve abstraction. User requires abstraction and how to achieve he is not bothered. Encapsulation is only for developer use.
Read the following points for more deeper study
1. In general, Abstraction lets you focus on what the object does instead of how it does it.
2. Encapsulation means hiding the internal details or mechanics of how an object does something. Like when you drive a car, you know what the gas pedal does but you may not know the process behind it because it is encapsulated.
3. Encapsulation means binding your data member (variable) function into one class (to one object). Abstraction means hiding your logic whatever you are used in your program.
4. Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing.
5. Abstraction involves the facility to define objects that represent abstract “actors” that can perform work, report and change their state and “communicate” with other objects in the system.
6. A real time example is as a driver you know how to start the car by pressing the start button and internal details of the starting operations are hidden from you. So the entire starting process is hidden from you otherwise we can tell starting operation is encapsulated from you or the driving wheel is encapsulated the process of rotating the wheel from you.
Before mentioning anything about abstraction, we can take three different users here (I am calling them as entity)
1) You 2) Local Mechanic 3) Expert
You Entity: Since you know only to start the car by pressing a button and all other operations behind the scene are abstracted from you.
Local Mechanic Entity: Our local mechanic knows some of the implementation of starting the car, i.e., he can open car’s bonnet and check the battery cable or choke etc. So in short Local Mechanic Entity knows some of the implementations of the car but not all.
Expert Entity: Since our expert (Designer of the car) mechanic knows all the operations of our car, he can repair it very quickly. So in short, Expert Entity knows all the implementations of the car.
The car’s operation is completely abstracted from you and it is partially implemented to Local Mechanic Entity and fully implemented to Expert Entity. So you are an abstract class having only abstract methods, Local Mechanic Entity has extended You (Since he is also an ordinary user) and he implemented some of the methods and last our expert Entity extending Local Mechanic and implementing all the methods.
8. In Simple words “Encapsulation is the feature of opps that is used to hide the data using Access Modifiers (public, default, protected, private)”. In OOPS programming language, object has a power to share certain properties or behaviour to outside package or outside class.
9. Abstraction is the feature used to hide the Complexity .
10. I would say that encapsulation is the process consisting of protecting internal instance state by only preventing the class user from altering this internal state except through the use of public methods (that of course handle the internal state in a consistent manner).
11. Abstraction is a mean to gather common attributes or methods in a same place, letting the ability to concrete subclasses to use them and to provide the implementation that is specific. Abstraction may also be used to use “abstract” objects with defined behavior with the ability to choose the actual implementation of this behaviour afterward. This is what may lead to think it has some similiarities with encapsulation (this notion of public API). However, in my opinion, this latter use of abstration in Java shall be replaced by the use of interfaces.
12. Thus, for me, in Java (and in more general context), encapsulation and abstraction are quite different notions, but they may be used together.
13. Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.
14. Abstraction solves the problem in the design side while Encapsulation is the Implementation. Encapsulation is the deliverable of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs.
15. Encapsulation is the way to achieve different level (class, object, method etc) of security if you like. Abstraction is the way to achieve extensible to the feature of any application (like a developer knows a method required but its functionality is not defined which will be defined when required.)
16. Abstraction:only expose the methods which are necessary. Encapsulation (Data Hiding) hides complexity
17. Encapsulation uses abstraction.
18. Encapsulation is hiding the data with a wrapper class. For this technique you need to modify the access identifier of the member variables or the inner classes,so that its hidden to external environment.
For eg : If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
19. The benefits are the fields of a class can be made read-only or write-only. A class can have total control over what is stored in its fields.
20. Abstraction: It’s the ability to make a class abstract. It’s functionality still exists similar to other classes but the class cannot be instantiated, as its way to abstract. You need to inherit the properties of the abstract class to initiate the subclass. Similarly, if you define abstract method, it’s signature is a bit different and need not require to write the implementation but the subclass must override the abstract method and provide the implementation specific to its own properties.
Pass your comments on this tutorial "Difference Encapsulation Abstraction" to improve the quality.