Tuesday, 7 April 2015

Encapsulation in java

Encapsulation is one of the four fundamental OOP concepts. The other three being -

Encapsulation Concept

According to the concept of Encapsulation, it is a technique to keep together the implementation (code) and the data it manipulates (variables). Having proper encapsulation ensures that the code and data both are safe from misuse by outside entity.

Encapsulation in Java

It is a Java class which is the foundation of encapsulation in Java. When a Java class is created it contains the code in methods and the member variables (instance variables). In a properly encapsulated Java class, method defines how member variables can be used. That access control to the methods and the variables is achieved through access modifiers (public, private, default & protected).

Each method or variable in the class may be provided public or private access modifier. Complete encapsulation in java is achieved by making members of a class private and access to them, outside the class, is provided only through public methods (getters and setters in this case).

Then there may be private methods having logic that is specific to that class and there may be public methods that are accessible to the other classes. Sine the private members (variables or methods) of a class may only be accessed with in the class, no improper outside access is possible.

As exp. -

Lets assume there is a class Employee which gets data about the employee from the DB, So we can have a getEmployeeData() method as public which can be accessed from outside. Let's assume there are separate lastName and firstName fields but the user wants it concatenated as full name so there is also a private method getFullName() which will concatenate them.

There will be another EmployeeBean class with the fields and proper getters and setters.

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());
  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();
  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


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 Encapsulation

  • To maintain code that is changed frequently by keeping that in one place thus providing maintainability and flexibility.
  • Encapsulation allows you to control the access to the class.

Points to note -

  • Encapsulation is one of the four fundamental OOPS concept. Other being, inheritance, polymorphism and abstraction.
  • Encapsulation means keeping code and data together in a class.
  • An encapsulated class will have private instance variable and only the methods within that class can access those variables.
  • Outside classes will be given access to any particular class through one or more public methods only.
  • Encapsulation in Java is achieved through the access modifiers- public, private, protected and default.

That's all for this topic Encapsulation in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Difference between Encapsulation and Abstraction in Java
  2. Method overriding in Java
  3. Class in Java
  4. Polymorphism in Java
  5. Abstraction in Java
  6. Java OOP interview questions

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment