Encapsulation in Java

Encapsulation is one of the four foundamental fundamental principles (Inheritance, Polymorphism, Abstraction, Encapsulation) of OOP. It is a principle of wrapping data and code that acts on that data together in a single unit.

Encapsulation forces the variables of a class to be hidden from other classes. They can be accessed only through the methods of their current class. So it is also known as data hiding.


1. How to Encapsulate a class ?

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to manipulate those private data.

Setter methods look like setXXX() where XXX is the name of the variable and are used to set the value of variables. Getter methods look like getXXX() where XXX is the name of the variable and are used to access the value of variables. They are generally public but you can use other access modifiers as well.

An Example would to appropriate to understand this:

Output :

Any attempt to access variables of Student s in ways other than setters and getters would result in a compilation error.

2. Why Encapsulation ?

Now let’s understand why Encapsulation is needed in the first place.

Output :

In the example above:

  • Data member salary is private. So it cannot be accessed directly.
  • If you try to set any illegal value to the variable salary, the logic inside setter setSalary() doesn’t allow this.

The final result is that the class is more safe and any mis-use is less likely to happen.


So the entire idea of Encapsulation centers around this: if the data members are directly visible to the outside world then anyone can modify the data in any illegitimate way but if you allow access to data members through methods then you can set the logic to discard any misuse.


3. Advantages of Encapsulation

  • As shown above, it avoids any misuse of the data members.
  • An encapsulated class can be easily unit tested.
  • Nowadays, it is faster to create an encapsulated class. IDEs provide easy  ways to insert setter and getter methods. Moreover, it also falls in category of standard practices to follow while designing classes.
  • It is easy to make variables read-only. Just make them private and don’t provide any setter methods. Same goes for write only variables. Just make the variables private but this time don’t provide the getter methods.

4. Encapsulation vs Abstraction

Sometimes people get confused in these two features of OOP. They are related and work together but they have some differences.

  • Encapsulation solves implementation issues while Abstraction deals with design issues.
  • Encapsulation groups together data and the methods that act on it. Abstraction hides the interal implementation details and only provides the interface for interaction.
  • Encapsulation deals with data hiding while Abstraction deals with detail hiding.
  • Encapsulation is achieved by acess modifiers and and packages. Abstraction is achieved through interfaces and abstract classes.

5. Tools of Encapsulation

Encapsulation of classes is achieved mainly by access modifiers and packages.

    • Packages : A package is a collection of classes, interfaces and other packages. Packages are both naming and visibility control mechanism. We can define classes inside a package that are not accessible to code outside that package.
    • Access modifiers : They are the real tools with which access to a member is restricted.

6. More examples

  • A read only Class

Output :

This class has no setter method. So name can be set only at the time of object creation using the constructor. After that it is fixed, it cannot be modified.


  • A write only Class

Output :

Fruit class has only setter method and the variable is private. So variable name can only be set and by no means can be accessed. So Fruit is essentially a write-only class.