Sunday, October 14, 2018

Encapsulation in Java

Encapsulation concept

The concept of encapsulation is to keep together the implementation (code) and the data it manipulates (variables). Having an adequate encapsulation ensures that the code and data are protected against misuse by external entities.

Encapsulation in Java

The foundation of the encapsulation in Java is a Java class. In a class, you will have both methods and the instance variable. Because it is the code in the method that operates on the variables, in a suitably encapsulated Java class, the method must define how the member variables can be used.

This access control to the methods and variables is obtained through access modifiers (public, private, default & protected).

Each method or variable in the class can be marked as public or private to guarantee access control. A method or variable marked as public is accessible to other classes. A method or variable marked as private is internal to the class.

In a suitably encapsulated Java class, only mark the methods as audiences that must be accessed by other classes. The complete encapsulation in java is obtained through variables from a private class and access to them, outside the class, is provided only through public methods (getters and setters, in this case). Any method with specific logic for that class and used only by other methods of this class should be marked as private.

As the private members (variables or methods) of a class can only be accessed in the class, no inappropriate external access can be accessed.


Encapsulation Java sample code

Suppose there is an Employee class that obtains employee data from the database. In the Java class, there is a getEmployeeData () method marked as public, which can be accessed from the outside.

Suppose that, when you obtain data from the database, there are separate lastName and firstName fields, but the user wishes it to be concatenated as a full name. Since the logic of concatenating the fields lastName and firstName is specific to the class, you can have a private method getFullName () to be concatenated.

There will be another EmployeeBean class with the fields, you can see that all the fields are marked as private and getters and setters are used to access these fields.

Interested in java training ..Enroll now:java training in chennai

Let's go through the code to make it clear -

Employee Class

public class Employee {

 public EmployeeBean getEmployeeData(String empId){
  System.out.println("Going to DB to get employee Data");
  // goes to the stub method rather than going to DB
  EmployeeBean employeeBean = getData();
  //Will give Compilation error, data can be set only through setters method
  //employeeBean.age = 10;
  String name = getFullName(employeeBean.getFirstName(), employeeBean.getLastName());
  employeeBean.setFullName(name);
  System.out.println("Employee bean - " + "Age - " + employeeBean.getAge() +
    " Name - " + employeeBean.getFullName() + " Emp ID - " + employeeBean.getEmpId());
  return employeeBean;
 }
 // private method to concatenate first and last name
 private String getFullName(String firstName, String lastName){
  return firstName + " " + lastName;
 }

 // Stub method
 private EmployeeBean getData(){
  EmployeeBean employeeBean = new EmployeeBean();
  employeeBean.setEmpId("E001");
  employeeBean.setLastName("Mishra");
  employeeBean.setFirstName("Pyaremohan");
  employeeBean.setAge(35);
  return employeeBean;
 }

}
Employee Bean Class

public class EmployeeBean {
 private String lastName;
 private String firstName;
 private String fullName;
 private String empId;
 private int age;
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getEmpId() {
  return empId;
 }
 public void setEmpId(String empId) {
  this.empId = empId;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getFullName() {
  return fullName;
 }
 public void setFullName(String fullName) {
  this.fullName = fullName;
 }

}

Test Class

public class EmployeeTest {

 public static void main(String[] args) {
 
   Employee employee = new Employee();
   //employee.getData(); // WILL GIVE COMPILATION ERROR
   employee.getEmployeeData("E001");
 }

}
So it can be seen how in EmployeeBean all the variables are private and access is provided through setters and getters methods.

In class Employee there is getEmployeeData() method which can be accessed from outside. Since getFullName() has some logic specific to the class so that is made private.

Benefits of the encapsulation

• Maintain the code that is changed frequently, keeping it in one place, providing ease of maintenance and flexibility.
• The encapsulation allows controlling access to the class.

Points to be observed

• Encapsulation is one of the four fundamental OOPS concepts. Another being, inheritance, polymorphism and abstraction.

• Encapsulation means keeping code and data together in a class.

• An encapsulated class will have a private instance variable and only the methods within that class can access these variables.

• External classes will have access to any particular class through one or more public methods.

• Encapsulation in Java is obtained through public, private, protected and standard access modifiers.





No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...