Data Encapsulation and Abstraction in java-


Encapsulation means "to put something in a capsule (a wrapper)", so that it is not directly accessible to outside world.

For example like medicine capsule, the medicine within capsule wrapper is not accessible until capsule is dipped in some liquid. I.e. to access medicine within capsule wrapper we have to follow certain procedure (rules and regulations).

In the same way Data Encapsulation means to put data (variables) in some wrapper so that it is not directly accessible to outsiders. The wrapper here will be the private access specifiers i.e. declare variables as private so that they are not accessible outside the class. This process of hiding data from outsiders is known as Data Hiding. And if any user outside the class wants to access encapsulated data then he has to follow certain rules and regulations and these rules and regulations are defined by public methods.

Abstraction means showing only essential details by hiding unnecessary details. For example when user is doing an online transaction, he does not need to know internal working of how transaction is taking place (unnecessary details for user), he is provided with an interface (essential detail) by which he can easily made his transaction.

Abstraction means hiding the detail at design level and Encapsulation means hiding the detail at implementation level.

Example -


class Employee
{
 private int id;
 private String name;
 private int salary;

 public void setId(int i) 
 {
  if(i<=0)
  {
   throw new RuntimeException("ID should be greater than zero");
  }
  else
  {
   id = i;
  }
}
    
public int getId() 
{
 return id;
}

public void setName(String n) 
{
 name = n;
}

public String getName() 
{
 return name;
}

public void setSalary(int s)
{
 if(s<=0)
 {
  throw new RuntimeException("Salary should be greater than zero");
 }
 else
 {
  salary = s;
 }
}
    
public int getSalary() 
{
 return salary;
}
}

public class Demo
{
 public static void main(String args[]) 
 {
  Employee emp1 = new Employee();
  emp1.setId(1);   
  // Accessing data using public methods b/c data is encapsulated by making them private.
  
  emp1.setName("Tom");
  emp1.setSalary(20000);

  System.out.println("Employee details :"+ emp1.getId()+"  "+emp1.getName()+"  " + emp1.getSalary());
  }
 }


    

Output -

Employee details :1 Tom 20000

In the above program we declare all data (id, name, salary) of the Employee class as private so that anyone outside the class should not be able to access it directly. This process of wrapping the data in private access specifier is known as Data Encapsulation.

If someone wants to access this encapsulated data then he has to follow certain rules and regulations, and we have declared these rules and regulations by defining methods (for example if user enter id of employee less than one then a Runtime Exception object will be thrown showing the message 'ID should be greater than zero' and programs terminates there).

Right now, you do not need to be worried about what Exception is,You will learn it later.For now just consider that it will stop program execution and show the message whatever you entered in double quotes.).

The rules and regulations defined for accessing encapsulated data is known as Data Abstraction.

Benefit of Data Encapsulation -

So now the question arises why we encapsulate data, and what is the benefit of doing this?
By making data encapsulated, we ensure that anyone outside the class should not be able to access the data, if he passes some anonymous values for data for example we know that Employee id can never be 0 or negative similarly salary can also never be zero or negative. So if user ever passes some negative values or zero for these data then he will not be able to access them and a Runtime Exception will be thrown.