Java

Encapsulation in Java | Explained

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:

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:

package javaencapsulation;
public class Employee {
  private int employeeId;
  private String employeeName;
}
class JavaEncapsulation {
public static void main(String[] args) {
  Employee emp = new Employee();
  emp.employeeId = 1;
  emp.employeeName = "Micheal";
 }
}

The screenshot of the code is provided below:

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:

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;

public class Employee {

 private int employeeId;
 private String employeeName;
    public void setId(int id)
    {
        this.employeeId = id;
    }
    public void setName(String name)
    {
        this.employeeName = name;
    }
    public int getId()
    {
        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;

public class Main {
  public static void main(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:

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

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.