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
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.
================== For Extra Reading ====================