Advantages of encapsulation

In a recent post, we have discussed the encapsulation in java and with the encapsulation in java example. But what are the advantages of encapsulation? Why we should use java encapsulation?
In this post, we will explain all the advantages of encapsulation and also cover them encapsulation example.

Advantages of encapsulation

Advantages of encapsulation

1. Improve flexibility and maintainability

In java, Encapsulation binds the data and action together. Using encapsulation, variable, and method can wrap together in a single unit. To achieve encapsulation, all variables must be private, and they should be handled by the only method in the class. It improves maintainability and flexibility and re-usability of code.
Let’s take an example of the student class. Here we are declaring all variables as private and creating the setter and getter to handle them.

class Employee
{
    // For encapsulation all variables should be private
    private String name;
    private int empId;
    private String department;
    private int age;
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
public class EncapsulationExample
{
    public static void main(String args[])
    {
    	Employee emp1 = new Employee();
    	emp1.setName("Ram");
    	emp1.setEmpId(101);
    	emp1.setAge(27);
    	emp1.setDepartment("IT");
    	
    	Employee emp2 = new Employee();
    	emp2.setName("Krishana");
    	emp2.setEmpId(105);
    	emp2.setAge(25);
    	emp2.setDepartment("Management");
    	
    	System.out.println("Name : "+emp1.getName() + ", Age : "+emp1.getAge());
    	System.out.println("EmpId : "+emp1.getEmpId() + ", Department : "+emp1.getDepartment());
    	
    	System.out.println("Name : "+emp2.getName() + ", Age : "+emp2.getAge());
    	System.out.println("EmpId : "+emp2.getEmpId() + ", Department : "+emp2.getDepartment());
    } 
}

Output: Name : Ram, Age : 27
EmpId : 101, Department : IT
Name : Krishana, Age : 25
EmpId : 105, Department : Management

In the above example, all the variables and codes implemented by setter and getter. You can change the implementation of any setter or getter at any point in time. Because implementation is hidden for outside classes, they just using the setter and getter to access the private field. Suppose if make any change in setEmpId() method(Setter) we just need to update the inner code of method and it would not break the classes that use the code.

2. Provide more security

By use of encapsulation, you can provide more security to class. You can make
the fields in read-only mode or write-only mode. Let’s take an example that will help you to understand the use of read-only or write-only mode.

Scenario 1 for read mode

Suppose an example of the Employee records. Each employee has an id that must be created when the employee joins the company. The id remains the same till he does the job. If we create it in a java program, then id should be created only when we are creating an object of the employee

class Employee
{
    // For encapsulation all variables should be private
    private String name;
    private int empId;
    private String department;
    private int age;
    
    // Creating a constrcutor that will set empId during creation of employee onbject 
    Employee(int empId)
    {
    	this.empId = empId;
    }
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
public class EncapsulationExample
{
    public static void main(String args[])
    {
    	Employee emp1 = new Employee(101);
    	emp1.setName("Ram");
    	emp1.setAge(27);
    	emp1.setDepartment("IT");
    	
    	Employee emp2 = new Employee(105);
    	emp2.setName("Krishana");
    	emp2.setAge(25);
    	emp2.setDepartment("Management");
    	
    	System.out.println("Name : "+emp1.getName() + ", Age : "+emp1.getAge());
    	System.out.println("EmpId : "+emp1.getEmpId() + ", Department : "+emp1.getDepartment());
    	
    	System.out.println("Name : "+emp2.getName() + ", Age : "+emp2.getAge());
    	System.out.println("EmpId : "+emp2.getEmpId() + ", Department : "+emp2.getDepartment());
    } 
}

Output: Name : Ram, Age : 27
EmpId : 101, Department : IT
Name : Krishana, Age : 25
EmpId : 105, Department : Management

Here we are not providing the setter for empId field. The value of empId field only initialized when we create an object of Employee. We will provide the getter of empId only. There is no other way to set the value of empId field because empId is private.

Scenario 2 for write mode

Let’s say you do not want to provide the read access of the age field. So, you can remove the getter of the age field. It will restrict the read access of the field because the field is private.

class Employee
{
    // For encapsulation all variables should be private
    private String name;
    private int empId;
    private String department;
    private int age;
    
    // Creating a constrcutor that will set empId during creation of employee onbject 
    Employee(int empId)
    {
    	this.empId = empId;
    }
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
}
public class EncapsulationExample
{
    public static void main(String args[])
    {
    	Employee emp1 = new Employee(101);
    	emp1.setName("Ram");
    	emp1.setAge(27);
    	emp1.setDepartment("IT");
    	
    	Employee emp2 = new Employee(105);
    	emp2.setName("Krishana");
    	emp2.setAge(25);
    	emp2.setDepartment("Management");
    	
    	System.out.println("Name : "+emp1.getName() + ", Age : "+emp1.getAge());
    	System.out.println("EmpId : "+emp1.getEmpId() + ", Department : "+emp1.getDepartment());
    	
    	System.out.println("Name : "+emp2.getName() + ", Age : "+emp2.getAge());
    	System.out.println("EmpId : "+emp2.getEmpId() + ", Department : "+emp2.getDepartment());
    } 
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problems: The method getAge() is undefined for the type Employee The method getAge() is undefined for the type Employee at create.EncapsulationExample.main(EncapsulationExample.java:54)

It is throwing exception because getAge() method is not defined in class.

3. Protect class data

By use data hiding a program can be divided into objects with specific data and functions. We can create separate objects with unique data sets and functions, avoiding unnecessary penetration from other program classes.

Let’s take an example, how data hiding protects the details of class members from outside. Suppose you have a class that contains several interdependent fields(variables). So, you should take care that must be in a consistent state. If any programmer is manipulating those fields directly, it may break the consistency of those fields. The programmer may change only one field without changing important related fields.
But instead of this, we can make a method that will communicate to interdependent fields. If the programmer wants to change the field, he just needs to call a method and the method will update all fields.

4. Data hiding

By use of encapsulation, we can achieve the data hiding. It hides the implementation detail of the class from the outside user. The user only knows we how to pass the value to the setter and how to get the value from the getter. There is nothing too visible to the user about how the class is storing values in the variables.

Leave a Comment