Java Encapsulation Abstraction Data hiding and Data binding


Java Encapsulation : In an object-oriented programming language, Encapsulation, Abstraction, Data hiding and Data binding are very confusing words, everyone feels difficult to understand. Many Web sites discuss at length and finally one site contradicts slightly with the other with many user comments of what they feel.
Here I give in my own style of explanation on Java Encapsulation etc., first in a layman way (to simplify the complexity) and then technically.

When a male and female get married they are known as husband and wife and they are binded (actual English grammatical usage must be "bound") together (here I use binding word). The advantage of binding is their secrets of life are not revealed to others, other way they are hidden (I use the word hidden). The behaviour between them is hidden from outside world. They may donate some money to charities. Charities are using the money but without knowing how they saved the money out of their salary. I say the saving style is abstracted from charities (here I used the word abstraction). But charities are still using the money without knowing the implementation details of saving. Like this pair of wife and husband, many pairs exist. One pair’s behaviour is not known to the other pair, other way, one pair secrets of life are known to the other pair. Each pair makes one unit. I say units are encapsulated (here, I use encapsulation). With encapsulation, one unit (pair) salary and saving data cannot be known or changed or affected by the other. Encapsulation is possible between the pairs residing in the same building or locality.

Similarly, in the house you are using a Television set (TV). You are able to see the pictures and able to change the channels without knowing electronics ICs and circuit boards. I say the making of TV (implementation details) is abstracted (hidden) from you. To make TV, you should be an electronics engineer but to use a TV, you need not be an electronic engineer. Similarly, you are using printf() function in C-lang without knowing the code of printf(). I say printf() implementation details are abstracted from you. In true sense, abstraction and hidden looks similar but slightly varies in their usage.

I apply the above discussion to a Java class. There is an Employee class with many objects. All employees have salary. One employee salary is not known to the other employee or one employee cannot change other employee salary. Let us discuss the above terms through employee example.

public class Employee
{
  public int salary;
  public static void main(String args[])
  {
    Employee emp1 = new Employee();
    Employee emp2 = new Employee();

    emp1.salary = 5000;
    emp2.salary = 6000;

    System.out.println("emp1.salary Rs." + emp1.salary);     // prints 5000
    System.out.println("emp2.salary Rs." + emp2.salary);     // prints 6000
  }
}

Java Encapsulation

salary is an instance variable which gives the property of salary paid to an Employee object. The salary of Employee emp1 is assigned as 5000. This assignment is done by calling the emp1 object with instance variable salary as emp1.salary. When called, both emp1 and salary are binded together (known as data binding). When binded they occupy one location in the memory. Similarly emp2.salary is assigned as 6000 and this emp2.salary occupies different location in memory. One location is not known to another even though both objects emp1 and emp2 belong to the same class and there is only one instance variable. That is, emp1 does not know the salary of emp2 and likewise emp2 is not aware of emp1 salary. In other way, they are hidden from each other and is known as data hiding. Data hiding is possible due to data binding, other way impossible. This is known as encapsulation. emp1.salary is encapsulated from emp2.salary and similarly emp2.salary is encapsulated from emp1.salary. The advantage of encapsulation is emp1 cannot alter the emp2 salary and vice versa. Other classes can make use of emp1 salary of 5000 without knowing how the salary is assigned (or implemented). Other way, the salary assignment information is abstracted for other classes, but still can make use of emp1 salary. The advantage of Java encapsulation is one instance variable can be given different values, of course, calling with different objects.

Also refer for more on Java encapsulation in the posting Three Great Principles – Data Binding, Data Hiding, Encapsulation.

Some classes would like to assign salary to emp1 object and they may give any amount of salary. To control this, public int salary can be made as private int salary. Being, private, other classes cannot access directly. For other classes access, a separate class should be made with setter and getter methods. Some people also call this as encapsulation. This type of encapsulation and how to use private variables and public methods is illustrated in Public methods and Private Variables.

How to explain before an interviewer the difference between Encapsulation and Abstraction?

================== For Extra Reading ====================

1. What are Features of Java?
2. Java Drawbacks
3. Data Types Default Values – No Garbage
4. Backslash in Java
5. Java Hello World Example

13 thoughts on “Java Encapsulation Abstraction Data hiding and Data binding”

  1. The definition of Abstraction is wrong,
    Abstraction is a verb, it is an activity of layering the system, creating generic\abstract classes hierarchy, ultimately achieving modules \ loosely coupled systems. Remember your class is always an instance of the top most class in the hierarchy, You are always a Human by abstraction on further abstraction your are a LivingThing. if you are male then your class hierarchy could be LivingThing—>Animal—>Vertebrate—>Mammal—>Human—>Male. depends on how deep you abstract your classes.

    Someone has asked the question about differences between Interface and Abstract classes.
    Interfaces are interfaces exposed for other layers \ systems to communicate with your module \ system. As explained above abstract classes are the output of abstraction activity used to achieve hierarchy of classes. All the classes above Male are abstract as there are nothing called Mammal or Human in nature they are just our grouping based on some common features.
    Interfaces can be used to add more roles. You might be a Male but at work you give an Employee interface, at to your parents a Son Interface and to your wife a more privileged Husband Interface. and therefore they are allowed to be implemented multiple. but not abstract classes as you cannot be both Animal and Bird by abstraction.

  2. Hello sir, some writers says that java is not a pure object oriented language and some says it is pure object oriented language. Is java pure object oriented language or not? If yes then why and if not then why?

  3. One more article about OOps and one more confusion. I have read so many article and all are with different opinion. I am not sure that, who had developed the concept, he was confused or other people are also confused.

    You started well with nice nice example but at the time of conclusion you confused us.

Leave a Comment

Your email address will not be published.