Encapsulation in Java


JavaViews 1672

Encapsulation in Java is another OOPs concept which represents data hiding. This means that the variables are encapsulated within the class and other classes cannot access them directly. We can access them only through the public class methods. In this way, we can hide important data and restrict their modification.

How to achieve encapsulation

We can achieve encapsulation in java in the below ways.

  • Declare variables as private. Please refer to access modifiers to understand more about private modifier. We cannot access private variables directly from other classes.
  • Implement getter and setter methods. We can use these to read and write the values.

Encapsulation in Java

 

Advantages of Java Encapsulation

  • Provides security by not allowing outside class to modify the private fields.
  • We can have either read-only or write-only methods also hence providing restrictions to other classes to access them. This means, if a class has only a get method, we can only read the variables. Similarly, we can have only a set method to only set the variable.
  • Provides flexibility and reusability through the getter and setter methods.
  • Implementation details are not visible to other classes.
  • Easy for unit testing

Now let’s understand the encapsulation concept in java using an example.

Example of  Java Encapsulation

In the below example, we have 2 private variables pname, dept in the Professor class. Since the variables are private we cannot access them outside the class. For this, we use the methods getProfName and getDeptName to retrieve the values of the variables pname and dept. Similarly, we use the methods setProfName and setDeptName to set the values of the variables name and id.

Now from College, using the Professor object we can access the variables using the public getter and setter methods. In this way, we can achieve encapsulation.

class Professor {
  private String pname;
  private String dept;
  
  //Getter methods
  public String getProfName() {
    return pname;
  }
  
  public String getDeptName() {
    return dept;
  }
  
  //Setter methods
  public void setProfName(String name) {
    this.pname = name;
  }
  
  public void setDeptName(String dept) {
    this.dept = dept;
  }
}
public class College {

  public static void main(String[] args) {
    Professor p = new Professor();
    p.setProfName("Mohan");
    p.setDeptName("CSE");
    System.out.println("Professor Name: " + p.getProfName());
    System.out.println("Department: " + p.getDeptName());

  }

}
Professor Name: Mohan
Department: CSE

Encapsulation: Read-only

As we saw in the benefits of encapsulation in java, we can use only the getter methods to read the values of the private variables. In this case, we cannot set the values as per our requirements, because we have not written any setter methods.

In the below read-only encapsulation class example, we can only read the variable value and we cannot set it. We have written only getAccountNumber method to read the account number value.

class Account {
  private int accountnumber = 4567890;
  
  public int getAccountNumber() {
    return accountnumber;
  }
}
public class Bank {

  public static void main(String[] args) {
    Account a = new Account();
    System.out.println("Account number: " + a.getAccountNumber());

  }

}
Account number: 4567890

Encapsulation: Write-only

In the below example, we have only a setter method, hence it is an example of write-only class encapsulation. Here we can only set the value and we cannot read it. This is another advantage of encapsulation.

class Account {
  private int accountnumber;
  
  public void setAccountNumber(int accno) {
    this.accountnumber = accno;
  }
}
public class Bank {

  public static void main(String[] args) {
    Account a = new Account();
    a.setAccountNumber(123456);

  }

}

Reference

Translate »