What is encapsulation?

Encapsulation is one of the basics of OOP. It is the principle of combining data (variables) and code into a single unit. In encapsulation, we hide all the unnecessary data from the outer world and provide the proper way (setter-getter methods) to access them.

How to achieve encapsulation?

To achieve encapsulation in Java, you need to follow some encapsulation principles-

  1. All the data-members of a class should have proper access-modifier.
  2. We should provide the required setter-getter methods to access data members.

Encapsulation example

In the previous tutorials, we have used class Fruit several times where all the data members are marked as public, now see the encapsulated version of the same class.

public class Fruit {

    // All private data members
    private String name;
    private String color;
    private double size;
    private double price;

    // Setter and getter method for name
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    // Setter and getter method for color
    public void setColor(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    // Setter and getter method for size
    public void setSize(double size) {
        this.size = size;
    }

    public double getSize() {
        return size;
    }

    // Setter and getter method for price
    public void setPrice(double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    // The constructor
    protected Fruit(String fName, String fColor, double fSize, double fPrice) {
        this.name = fName;
        this.color = fColor;
        this.size = fSize;
        this.price = fPrice;
    }

}

What is the setter and getter method?

The setter and getter method is the publicly exposed method to access the private data members of a class.

Setter method

A setter method should always start with the prefix ‘set’. Suppose for a private data member ‘name’ we can create a setter method like – setName(String).

A class containing only setter methods for the private data members are also known as a write-only class because there is no way to access those data members.

Example

public class SetterExample {

    private String name;

    // A setter method for name
    public void setName(String name) {
        this.name = name;
    }

}

Getter method

A getter method should always start with the prefix ‘get’ or ‘is’ (for boolean). Suppose for a private data member ‘name’ and ‘eligible’ we can create a getter method like – getName() and isEligible() respectively.

A class containing only getter methods for the private data members are also known as a read-only class because there is no way to update the value for those data members.

Example

public class GetterExample {

    private String name;
    private boolean eligible;

    // A getter method for name
    public String getName() {
        return name;
    }

    // A getter method for eligible
    public boolean isEligible() {
        return eligible;
    }

}