Encapsulation in Java

Encapsulation in Java is a mechanism to binding the variables (data) and methods(code) together as a single unit. It is the process of hiding information details and protecting data and behavior of the object.

Encapsulation is about protecting the internal state of an object and controlling how it’s accessed and modified. The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users.

Example of Encapsulation in java:

In Java, every class you create is a example of encapsulation , here the basic example of Encapsulation given below:

 class Person {
    String name;
    int age;
    void eat() {
    void drink() {

In this example we binds the variables(name, age) and methods(eat , drink) in a single unit which is person class. it is a basic example of encapsulation.

The Person class hides its attributes like name and age and encapsulates its behaviors like eat() and drink(). To access its data and behavior, other classes must create a Person object as shown below:

 Person p = new Person();
 p.name = "kamal";

How to achieve encapsulation in Java:

The encapsulation process ensures that the data and behavior of a unit can not be accessed directly from other unit.

Encapsulation in Java is achieved by following these steps:

  • Declare class variables/attributes as private
  • Provide public getter and setter methods to access and update the value of a private variable

Now we will explore another form or extension of encapsulation which is data hiding:

What is Data hiding in Java:

Data hiding in Java means keeping the inner workings and details of an object hidden from the outside world .The purpose of data hiding is to protect the data from misuse by outside world.

In java we use private access modifier to protect or hide the data and behavior from outside world. For example if I declare the Person class variables like below :

class Person {
   private String name;
   private int age;   

In this class, name and age are declared as private, which means they can’t be directly accessed or changed from outside the class. These variables will be visible within the class only.

if you make person class object and try to access these variable data it will result compilation error

class XYZ {
   Person p = new Person();
   p.name = "kamal"; // compilation error, as name attribute won't be accessible here   

Now the question is , what if other classes wants to read and write these data ? because if other classes can’t use access this class data, then this class won’t be useful.

So in order to allow these data to be accessed/modify from outside we create getters(getXXX()) and setters(setXXX()) methods only for these variables.

How getter and setters methods works:

getter and setter methods help us to access and modify the private field (variable) of a class it prevent data misuse and illegal access.

  • getter(): it is used to only fetch the value of a private field ( read only).
  • setter(): it is used to set or update new value of a private field (write only).
Lets take an example here:
class Person {
    private String name;
    private int age;
    // Getter method for 'name' field
    public String getName() {
        return name;
    // Getter method for 'age' field
    public int getAge() {
        return age;
    // Setter method for 'name' field
    public void setName(String name) {  
        this.name = name;
    // Setter method for 'age' field
    public void setAge(int age) {
        this.age = age;
public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
        // Modify values using setter methods
        System.out.println("Updated Name: " + person.getName());
        System.out.println("Updated Age: " + person.getAge());

Name: Alice
Age: 30
Updated Name: Bob
Updated Age: 25

Advantage of encapsulation in Java:

Controlled Access: Encapsulation helps in controlling the access to the internal data of a class. You can decide which variables and methods are accessible from outside the class and which ones are not.

Data Protection: By hiding the internal details of a class, encapsulation protects the data from unauthorized access and modification

Flexibility: Encapsulation allows you to change the internal implementation of a class. you can modify the inner workings of the class without breaking other parts of the code.

Code Organization: You can arrange your code in a neat and organized way. You divide a complicated system into smaller, understandable classes, each handling a particular task.

Code Reusability: Encapsulated code or unit can be reused anywhere inside the application or across multiple applications. This reduces duplication of code and promotes a more efficient development process.

Security and Privacy: Sensitive information can be hidden within a class and only accessed through controlled methods. This helps in enhancing the security and privacy of the data,

Leave a Reply

Your email address will not be published. Required fields are marked *