Encapsulation vs. Abstraction

Difference Between Encapsulation and Abstraction

Abstraction and encapsulation are two different but related concepts found in OOP (Object Oriented Programming) language. Encapsulation is the concept of combining data and behavior together as a single entity. On the contrary, abstraction is the procedure of presenting an entity- how it behaves, looks versus how it is executed.

What is Encapsulation?

Encapsulation can be defined as the procedure of packing data and operations that operate on them into a single entity. This means that to access data, certain predetermined methods should be used. In other words, the data contained are not directly accessible. This ensures that data integrity is preserved because the user is unable to directly approach and modify the data as he / she wants. Users will receive or will the data values ​​only by methods that are publicly available to users. These methods usually provide validation data to only be allowed that the data in the appropriate format to be inserted into the fields. So the benefits of encapsulation are threefold. For encapsulation, the programmer can make the fields of a class read only or write only. Second, a class can have total control over what is stored in its fields. Finally, users of a class do not need to worry about how their data is stored. In Java, the programmer can declare any variable case to be private and provide and receive set methods (which are public) access and adapt private fields.

What is Abstraction?

Abstraction is the process of separating the details of presentation of the implementation details. This is done so that the developer is relieved of the complex details about implementation. The programmer can instead concentrate on the presentation or details of behavior of the entity. In simple terms, the abstraction focuses how a certain entity can be used rather than how it is executed. The abstraction hides the implementation details essentially, that even if the implementation methodology completely changes the time, the programmer should not worry how it would affect his program. A system can be reduced in several layers or levels. For example, the abstraction layers to reveal low-level details of hardware, while the high level abstraction to reveal only the business logic of the entity. The abstraction of terms can be used to refer to an entity as well as a process and this causes some confusion. Abstraction, as a process, means to extract important details and ignoring non-essential details of a particular item or a group of items as an entity; abstraction means a view or a model of an entity with just the important details. In Java, the programmer can use the abstract keyword to declare a class as an entity that represents the important features that define an entity in the real world.

What is the difference between encapsulation and abstraction?

Although Abstraction and Encapsulation are highly related concepts found in the object-oriented programming languages, they have major differences. Abstraction helps us identify what should be visible and hidden. Encapsulation is for packaging the information as it makes visible what should be visible and hide what should be hidden. That is, encapsulation is a step ahead of abstraction. Abstraction reduces an object to its essential defining characteristics; encapsulation extends this idea by placing and connecting the feature of this entity.

 

Category: VS  |  Tags: