encapsulation in java programs

Let’s learn encapsulation in java programs.

encapsulation in java programs

what is encapsulation?

encapsulation in java is a procedure of binding data or variables and methods together.

To achieve encapsulation declare variables of a class to private and add setter and getter methods to modify and view values of variables.

Let’s see java program on encapsulation.

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 for encapsulation in java variables ID, stuName and stuAge will be hidden from other classes.

Also read – polymorphism in java

These variables are accessed only through setter and getter methods of their current class. This theory is known as data hiding.

Real life or world example of encapsulation.

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