Encapsulation vs Abstraction

In this post, you will learn:

  • What is Encapsulation?
  • What is Data Hiding?
  • What is Abstraction?
  • What is the relation b/w Encapsulation, Data Hiding and Abstraction?

What is Encapsulation?

Encapsulation is the process of putting something into a capsule. In programming, Encapsulation represents the grouping of logically related data and operations into blocks called classes. In real life context, a company can encapsulate its stakeholders into the following groups according to their functionalities: Employee, Product, Customer, Supplier, Share holder etc.
Encapsulation serves following purposes:

  1. Logically related data and operations are grouped together in the form of an entity to simplify their management.
  2. It provides a mechanism of access control i.e. data hiding.

What is Data Hiding?

The process of hiding the data of an entity from other entities is called Data Hiding . It is made possible because of Encapsulation. The purpose of Data Hiding is to prevent corruption of data by other entities.

In real life context, All the things that we have in our houses can be thought of as our data. Different type of people such as unknown visitors, friends and relatives come to our house. We don’t want to show each and every item of our house to each person hence we apply different access rules to them.

Encapsulation isn’t Data Hiding.

Encapsulation is the means  (saadhan)  and Data Hiding is the end  (lakshaya) . Because of the one the other is possible. When we create a group we encapsulate, and when we apply different access rule on its members we do data hiding. We can’t hide data without groups but we can create groups without data hiding. These groups will act like public places such as roads, parks etc where all the persons have same access.

What is Abstraction?

Abstraction is the process of hiding the internal working of a process from its end user. In the context of programming, separating the signature of a method from its definition is called abstraction. When a programmer wants to perform a task, he looks for a method which can do it for him. If such a method is found, the programmer invokes it without bothering about its internal working.

Relation among Encapsulation, Data Hiding and Abstraction?

To understand the relation and differences among Encapsulation, Data Hiding and Abstraction, we need to understand their users i.e for whom these concepts are designed. As a programmer, either we write our own code or use the code of others (libraries, frameworks, and classes & methods of other team members) i.e. we play two roles: Creator & User.

When we play the role of the creator, we classify entities according to their functionality to simplify their management i.e we encapsulate. As part of the encapsulation, we also apply access control i.e. Data Hiding. We decide, which data and operations of an entity are to be exposed to other entities. When we play the role of the user, we simply search the classes & methods of others which can do the intended task. We are not interested in who wrote them, or what data and logic they use.

Real life analogy to explain Encapsulation, Data Hiding and Abstraction

A house can be thought of as an application. Drawing room, Bed room, Store room, Kitchen, Bath room etc can be thought of as its classes. According to the functionality, we put different items (data) in different part (classes) of the house such as Bed in bedroom, sofa and TV in drawing room, utensils and stove in kitchen, soap, shampoo, buckets in bathroom etc. This is encapsulation.

Most of the items are accessible to all the members of the house but some valuables such as money and jewelery are kept hidden in the closet i.e. neither all the members of the house nor outsiders have access to them. This is data hiding.

In our house we use various appliances such as TV, Fridge, Fan, AC etc. Neither we create them nor we have any interest in their internal workings. We simply operate them i.e. their internal working is abstracted from us. This is abstraction.

Its time to recap the main concepts:

  1. Encapsulation is the process of grouping logically related data and operations into units called classes.
  2. Data Hiding represents access control. It is made possible because of Encapsulation.
  3. Abstraction is the process of hiding the details of internal working of a process from its intended users.
  4. When we write our own code, we are interested in encapsulation and data hiding. When we use the code of others, we are interested in abstraction.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

6 thoughts on “Encapsulation vs Abstraction

  1. Java was initially developed for client side programming i.e. java applet.
    But Applets are not very popular now.
    Infact Java script , jquery , css etc. are very popular.
    Why Java applets failed?

    1. To understand the reason behind this you need to dig into the past. When Java was launched in 1995, It was supported by NetScape Navigator the first browser of the world i.e. Navigator had the built-in JRE to execute Applets.
      As alternative of Navigator, Microsoft launched I.E. which also supported Applets. By giving the I.E free as part of window-95, Microsoft captured 90% of the browser market i.e. Navigator was defeated comprehensively.
      In 1998, Microsoft launched Window 98 which also had free I.E.
      Support of JRE was dropped from it by Microsoft to promote its own client side scripting language VB Script.
      It leads to the legal battle b/w Microsoft & Sun Microsystems. When they were battling, Java Script came as alternative of both Applet as well as VB Script. Meanwhile, Sun Microsystems shifted focus to server side programming by the launch of Java 2 in 1998.
      Finally, When the verdict came in favor of Sun Microsystems, Microsoft provided JRE support in IE but to counter applet, provided support of Java Script as well. By the support of Microsoft, Java script became de-facto standard of client side programming. As a result, Sun Microsystems, also dumped it i.e. no new enhancement were provided and focused entirely on server side programming.

Leave a Reply

Your email address will not be published. Required fields are marked *