Encapsulation is one of the most significant concepts of object-oriented programming that provides security by hiding the sensitive data/implementation details of the class from the users. In Java, encapsulation can be achieved by declaring the class attributes/variables as private. In certain cases, we need to access or modify the private variables, so in such a scenario, we can use the public get and set methods.

This write-up provides a thorough overview of encapsulation in the following aspects:

  • What is Encapsulation in Java
  • Get and Set Methods in Java
  • Basic Syntax of Get and Set in Java
  • Implementation of Encapsulation in Java

So, let’s start!

Encapsulation in Java

Let’s consider an example of a capsule to understand the basic concept of encapsulation in Java. When we look at a capsule we don’t have any idea what’s inside the capsule i.e. its ingredients are hidden from us. In the same way, encapsulation performs in Java i.e. it allows us to hide the attributes of one class from the other classes.

In order to work with encapsulation in Java, we have to declare the class attributes as private and the attributes of one class wouldn’t be visible or accessible to the other classes.

Implementation of Encapsulation in Java

The below-given examples will provide a detailed understanding of encapsulation in Java.

Example

Let’s consider the following piece of code, it has some restricted class attributes and we will try to access them from some other class:

packagejavaencapsulation;


publicclassEmployee {


privateintemployeeId;

private String employeeName;

}


classJavaEncapsulation {


publicstaticvoidmain(String[] args) {


  Employee emp = new Employee();


emp.employeeId = 1;


emp.employeeName = “Micheal”;


 }

}

The screenshot of the code is provided below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-21.png" data-lazy- height="261" src="data:image/svg xml,” width=”669″>

From the above snippet it is clear that we can’t access the restricted class attributes of Employee class.

Let’s resolve this problem, to do so we have to utilize the get and set methods for each attribute.

Get and Set Methods in Java

As the name itself suggests the setter/set method can be used to set the values of variables while the getter/get method is used to get the variable’s value. To access the private attributes of one class from some other class we have to set the type of getter and setter as public for every attribute of the class.

The syntax of the get and set method is shown in the below-given diagram:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-23.png" data-lazy- height="294" src="data:image/svg xml,” width=”680″>

In the above snippet, the setter method utilizes the “this” keyword which refers to the current object. Let’s have a look at an example to have a clear understanding of how getter and setter methods work.

Example

We have created two different java files which include two classes.

Employee.java

We created an Employee class and specify class attributes as private and to provide access to the outsider classes we create get and set methods for each private variables:

package employee;

publicclassEmployee {

privateintemployeeId;

private String employeeName;


publicvoidsetId(int id)


    {

this.employeeId = id;


    }


publicvoidsetName(String name)


    {

this.employeeName = name;


    }


publicintgetId()


    {

return(employeeId);


    }  

public String getName()


    {

return(employeeName);


 }  

}

Main.java

We create another class Main from where we will try to access attributes of Employee class:

package employee;

publicclassMain {


publicstaticvoidmain(String[] args) {


  Employee emp = new Employee();


emp.setId(1);


emp.setName(“Micheal”);

System.out.println(emp.getId());

System.out.println(emp.getName());


 }  

}

Following will be the output:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-24.png" data-lazy- height="413" src="data:image/svg xml,” width=”693″>

From the output it is clear that using getter and setter methods, we successfully access the private attributes of the Employee class from the Main class.

Conclusion

In Java, encapsulation provides security by means of hiding sensitive data including class attributes and member functions from other classes. To achieve encapsulation in Java, we have to specify the class attributes as private and to access or modify these private variables from some other class, we can use the getters and setters as public for each of the class private variables/attributes. This article presents a comprehensive guide for what is encapsulation and how to achieve encapsulation in Java.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/FA9AFCF66E2D44C3A65378AC87A87EB4-150×150.jpg6225493799e38.jpg" height="112" src="data:image/svg xml,” width=”112″>

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.