encapsulation in java

Let’s learn encapsulation in java.

encapsulation in java

what is encapsulation?

encapsulation is a procedure of binding data(variables) and methods together into a single unit.

encapsulation is one of the four fundamental Object Oriented Programming(OOPS) concept. Other three oops concept are polymorphism, inheritance and abstraction.

In encapsulation variables and methods in a class is hidden from other classes and accessed only through the methods within its own class in which the class it is declared.

Here data encapsulation means data in a class is hidden from other class by making variables and methods of a class private(access modifier).

To achieve encapsulation declare variables of a class as private and create getter and setter methods to modify and view values of variables. Let’s see an example for encapsulation in java.

class EncapsulationExample
{
   private int ID;
   private String stuName;
   private int stuAge;
   // getter and setter methods
   public int getStudentID()
   {
      return ID;
   }
   public String getStudentName()
   {
      return stuName;
   }
   public int getStudentAge()
   {
      return stuAge;
   }
   public void setStudentAge(int number)
   {
      stuAge = number;
   }
   public void setStudentName(String number)
   {
      stuName = number;
   }
   public void setStudentID(int number)
   {
      ID = number;
   }
}
public class ExampleForEncapsulation
{
   public static void main(String[] args)
   {
      EncapsulationExample student = new EncapsulationExample();
      student.setStudentName("Virat");
      student.setStudentAge(5);
      student.setStudentID(2353);
      System.out.println("Student Name: " + student.getStudentName());
      System.out.println("Student ID: " + student.getStudentID());
      System.out.println("Student Age: " + student.getStudentAge());
   }
}


Output:

Student Name: Virat
Student ID: 2353
Student Age: 5

In the above example variables ID, stuName and stuAge will be hidden from other classes.

These variables are accessed only through setter and getter methods of their current class. This theory is known as data hiding. Now let’s see encapsulation real time or real life example in java.

class ABCBank
{
   // private data members
   private long accountNumber;
   private String name, emailID;
   private float money;
   // getter and setter methods
   public long getAccountNumber()
   {
      return accountNumber;
   }
   public void setAccountNumber(long accountNumber)
   {
      this.accountNumber = accountNumber;
   }
   public String getName()
   {
      return name;
   }
   public void setName(String name)
   {
      this.name = name;
   }
   public String getEmailID()
   {
      return emailID;
   }
   public void setEmailID(String emailID)
   {
      this.emailID = emailID;
   }
   public float getMoney()
   {
      return money;
   }
   public void setMoney(float money)
   {
      this.money = money;
   }
}
public class RealWorldEncapsulationExample
{
   public static void main(String[] args)
   {
      ABCBank obj = new ABCBank();
      // setting values through setter methods
      obj.setAccountNumber(1112316548);
      obj.setName("Virat");
      obj.setEmailID("virat@gmail.com");
      obj.setMoney(2300000f);
      // getting values through getter methods
      System.out.println("Account Number: " + obj.getAccountNumber());
      System.out.println("Name: " + obj.getName());
      System.out.println("Email ID: " + obj.getEmailID());
      System.out.println("Money: " + obj.getMoney());
   }
}


Output:

Account Number: 1112316548
Name: Virat
Email ID: virat@gmail.com
Money: 2300000.0


Advantages of encapsulation in java

Here are benefits of encapsulation,

  1. Control access to data: by hiding the variables a user just know how to pass values to setter method and variables.
  2. Flexibility: Here we can make variables of a class as read only or write only by omitting setter method or getter method.
  3. Reusability: we can reuse the code depending on the requirement. Also validating values before modifying.
  4. Testing code: is easy.

Also read – major features of java