2011-11-27

How can we develop Encapsulation technically in Java ?

Ok, first review encapsulation definition again " Hiding class data from the outside world and accessing it only through publicly exposed methods".
It means restricting direct access of class variables to outside class members and providing accessibility to those variables through public methods is called encapsulation.
In Java encapsulation can be implemented

By declaring variables as private, to restrict direct access and
By defining one pair of public setter and getter methods to give access to private variables

The Rule on private access specifier members (variables and methods) is, they cannot be accessed from outside world of this class directly. Voilation leads to compile time error.

The below program explains developing a class by following encapsulation principle

// Bank.java
public class Bank
{
 //hiding class data
 private static final double MIN_BALANCE=5000;

 private long accno;
 private String username;
 private String password;
 private double balance;
 
 public Bank(long accno,String username,String password,double balance)
 {
  this.accno = accno;
  this.username = username;
  this.password = password;
  this.balance = balance;
 }
 //giving access to outside of the class members

 public void setAccno(long accno)
 {
  this.accno = accno;
 }
  
 public long getAccno()
 {
  return accno;
 }
 
 public void setUsername(String username)
 {
  this.username = username; 
 }

 public String getUsername()
 {
  return username;
 }
 
 public void setPassword(String password)
 {
  this.password = password;
 }
 
 public String getPassword()
 { 
  return password;
 }
 
 public void setBalance(double balance)
 {
  this.balance = this.balance + balance;
 }

 public double getBalance()
 {
  //add validation logic,if needed
  return balance;
 }
}
//Encapsulation.java
class Encapsulation
{
 public static void main(String[] args)
 {
  Bank iciciBank = new Bank(1,"siva","chaitanya",999999999);

  //accessing private variables directly from this class is not possible it leads to CE
  //System.out.println(iciciBank.balance);
  //System.out.println(iciciBank.username);
  //System.out.println(iciciBank.password);
  

  //access them through getXxx() methods 
  System.out.println(iciciBank.getUsername());
  System.out.println(iciciBank.getPassword());
  System.out.println(iciciBank.getBalance());

  //setting the variables with different values using setXxx() method
  iciciBank.setBalance(1000);
  
  iciciBank.setUsername("Pond James Pond");
  iciciBank.setPassword("siva chaitanya");
  
  System.out.println(iciciBank.getUsername());
  System.out.println(iciciBank.getPassword());
  System.out.println(iciciBank.getBalance());
 }
}
The advantage in providing variable access via setter and getter methods is we can authenticate user with proper authentication logic before setting or returning variable value. In the above program before giving balance details, in getBalance() method we can authenticate user. It is not possible if we provide direct access to balance variable.Here in the above example we have to execute Encapsulation program because it contains main method.

Loading

Enter your Email here: