Encapsulation in Java with Example

< Previous Next >

OOPS Tutorial

In this article, we will learn what is Encapsulation in Java with real-world examples and source code examples.
Encapsulation refers to combining data and associated methods as a single unit. In this post, we will learn Encapsulation in detail with examples.

1. Definition 

Encapsulation refers to combining data and associated functions as a single unit. In OOP, data and functions operating on that data are combined together to form a single unit, which is referred to as a class.
Encapsulation is the technique whereby the object's state is hidden from the outer world and a set of public methods for accessing this state are exposed. When each object keeps its state private inside a class, we can say that encapsulation was achieved. This is why encapsulation is also referenced as the data-hiding mechanism.
Encapsulation has a number of important advantages such as loosely coupled, reusable, secure, and easy to test code.
In Java, encapsulation is implemented via the access modifiers - public, private, and protected.

Advantage of Encapsulation:

The main advantage of using encapsulation is to secure the data from other methods. 
For example, when we make fields private then these fields are only used within the class, but these data are not accessible outside the class.

2. Encapsulation Real-world Examples

  1. The capsule, it is wrapped with different medicines. In a capsule, all medicine is encapsulated inside the capsule.
  2. A Java Class is an example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

3. Implementation with Examples

Let's consider an example: a Cat class can have its state represented by fields such as moodhungry and energy. While the code external to the Cat class cannot modify any of these fields directly, it can call public methods play()feed(), and sleep() that modify the Cat state internally.
The Cat class may also have private methods that are not accessible outside the class, such as meow(). This is encapsulation.
public class Cat {

    private int mood = 50;
    private int hungry = 50;
    private int energy = 50;

    public void sleep() {
        System.out.println("Sleep ...");

    public void play() {
        System.out.println("Play ...");

    public void feed() {
        System.out.println("Feed ...");

    private void meow() {

    public int getMood() {
        return mood;

    public int getHungry() {
        return hungry;

    public int getEnergy() {
        return energy;
The only way to modify the state (private fields) is via the public methods play()feed(), and sleep(), as in the following example:
public class Main {

    public static void main(String[] args) {

        Cat cat = new Cat();
        System.out.println("Energy: " + cat.getEnergy());
        System.out.println("Mood: " + cat.getMood());
        System.out.println("Hungry: " + cat.getHungry());
The output will be as follows:

Feed ...
Play ...
Feed ...
Sleep ...
Energy: 50
Mood: 53
Hungry: 49

4. Difference between Abstraction and Encapsulation

Abstraction and Encapsulation in Java are two important Object-oriented programming concepts and they are completely different from each other.
  • Encapsulation is a process of binding or wrapping the data and the codes that operate on the data into a single entity. This keeps the data safe from outside interface and misuse.
  • Abstraction is the concept of hiding irrelevant details. In other words, make the complex system simple by hiding unnecessary detail from the user.
  • Abstraction is implemented in Java using an interface and abstract class while Encapsulation is implemented using private, package-private, and protected access modifiers.
  • Abstraction solves the problem at the design level. Whereas Encapsulation solves the problem at the implementation level.
Encapsulation: Information hiding.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course