print
Advertisment
Advertisment

Encapsulation in java

Encapsulation is one of the four main concepts of the Oops concept, whose purpose is to always hide the sensitive data of the software so that no other person can access that data. Encapsulation is a process in which private data members or variables and methods are wrapped (bind) as a unit inside a class so that that data cannot be easily accessed by other person. Encapsulation is made up of data hiding and abstraction.

Advantage of Encapsulation

  • It is a way to achieve data hiding in Java because other classes will not be able to access the data through the private data members.
  • It provides you security through data hiding.
  • By providing only one setter or getter method, you can make the class read-only or write-only. it contains private variables, methods.
  • Encapsulation also improves the re-usability and is easy to change with new requirements.
  • The encapsulated class is easy to test. So, it is better for unit testing.

Normal Example

public class Encapsul{
  public String emp = "50 employee";
    public int sallary = 500000;
    public static void main(String[] args) {
      Encapsul obj = new Encapsul();
      System.out.println(emp);
      System.out.println(sallary);
  }
}

Output :

  
50 employee
500000
  

Example With Encapsulation

interface Employee{
   public void setEmp(String emp);
   public String getEmp();
   public void setSallary(int sallary);
   public int getSallary();
}
public class Company implements Employee{
  private String emp;
  private int sallary;
@Override
  public void setEmp(String emp){  //setter method
    this.emp = emp;
  }
@Override
  public void setSallary(int sallary){
    this.sallary = sallary;
  }
@Override
   public String getEmp(){
    return emp;
   }
@Override
   public int getSallary(){
    return sallary;
   }
  public static void main(String[] args) {
    Company obj = new Company();
    obj.setEmp("10 employee");
    obj.setSallary(200000);
    System.out.println(obj.getEmp());
    System.out.println(obj.getSallary());
  }
}

Output :

  
10 employee
200000
  
Advertisment

Example With Encapsulation

 public class Bank{
  private String bank_name;
  private String consumer_name;
  private int consumer_account;
@Override
public void setBank(String bank_name){     //setter method
  this.bank_name = bank_name;
}
@Override
  public void setName(String consumer_name){     //setter method
    this.consumer_name = consumer_name;
  }
@Override
  public void setAccount(int consumer_account){    //setter method
    this.consumer_account = consumer_account;
  }
@Override
  public String getBank(){    //getter method
  return bank_name;
}
@Override
   public String getName(){     //getter method
    return consumer_name;
   }
@Override
   public int getAccount(){     //getter method
    return consumer_account;
   }
   public static void main(String[] args) {
    Bank obj = new Bank();
    obj.setBank("PNB");
    obj.setName("XYZ");
    obj.setAccount(2109104575);

    System.out.println(obj.getBank());
    System.out.println(obj.getName());
    System.out.println(obj.getAccount());
   }
}


Output :

  
    PNB
    XYZ
  2109104575
  
Advertisment

Read-Only class

//A Java class which has only getter methods.
public class ReadOnly{
   private String city ="Agra"; //private data member
   public String getCity(){  //getter method for city
    return city;
  }
  public static void main(String args []){
    ReadOnly am = new ReadOnly();
    am.getCity();
  }
}

Output :

  
    Agra
  

Write-Only class

//A Java class which has only setter methods.
public class WriteOnly{
   private String city; //private data member
   public void setCity(String city){  //setter method for city
    this.city = city;
  }
  public static void main(String args []){
    WriteOnly am = new WriteOnly();
    am.setCity("Agra");
    System.out.println("City writed success");
  }
}

Output :

  
    City written success
  
Advertisment
Advertisment
arrow_upward