Difference Between Encapsulation and Abstraction

Encapsulation and Abstraction are two different but related concepts found in OOP (Object Oriented Programming) languages. Encapsulation is the concept of combining data and behavior together as a single entity. On the other hand, Abstraction is the process of presenting how an entity behaves\\looks as opposed to how it is implemented.

What is Encapsulation?

Encapsulation is the process of wrapping data and operation that operate on them in to a single entity. This essentially means that in order to access data, certain predefined methods should be used. In other words, encapsulated data are not directly accessible. This makes sure that the integrity of data is preserved because the user is unable to directly access and modify the data as he/she wishes. The users will get or set the data values only through the methods that are publicly available to the users. These methods usually provide data validation so that only the data in the appropriate format is allowed to be inserted in to the fields. Therefore, the benefits of Encapsulation are three-fold. Through Encapsulation, the programmer can make fields of a class read-only or write only. Secondly, a class can have total control over what is stored in its fields. Finally, the users of a class do not need to worry how its data are being stored. In Java, the programmer can declare all instance variable to be private and provide get and set methods (that are public) to access and modify the private fields.

What is Abstraction?

Abstraction is the process of separating the presentation details from the implementation details. This is done so that the developer is relieved of the more complex implementation details. Instead, the programmer can focus on the presentation or the behavioral details of the entity. In simple terms, abstraction focuses on how a certain entity can be used rather than how it is implemented. Abstraction essentially hides away the implementation details, so that even if the implementation methodology changes over time, programmer would not have to worry how it would affect his program. A system can be abstracted in several layers or levels. For example, low-level abstraction layers will reveal hardware details, while high-level abstraction will only reveal the business logic of the entity. The term abstraction may be used to refer to both an entity and a process and this leads to some confusion. As a process, abstraction means extracting essential details while ignoring the inessential details of an item or a group of items, while as an entity, abstraction means a model or a view of an entity with only the essential details. In Java, the programmer can use the keyword abstract to declare a class as an abstract entity, which represents the essential defining characterizes of a real world entity.

What is the difference between Encapsulation and Abstraction?

Even though Encapsulation and Abstraction are highly related concepts found in Object Oriented Programming languages, they have key differences. Abstraction is a technique, which helps us to identify what should be visible and what should be hidden. Encapsulation is the techniques for packaging the information such that it makes visible what should be visible and hides what should be hidden. In other words, Encapsulation can be identified as one step beyond abstraction. While abstraction reduces a real world object to its essential defining characteristics, encapsulation extends this idea by modeling and linking this functionality of that entity.