encapsulation is a way to hide data from another programmers, and to put restrictions on them to access it,
here is an example of set and get in java:
class Employee {
private int id;
private String name;
private double basicsalary;
private double allowances;
private boolean is_male;
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public double getSalary()
{
return basicsalary;
}
public double getAllowances()
{
return allowances;
}
public String getGender()
{
if(is_male==true)
return "male";
else return "female";
}
public void setId(int i)
{
id=i;
}
public void setName(String x)
{
name=x;
}
public void setBasicSalary(double v)
{
basicsalary=v;
}
public void setAllowances(double f)
{
allowances=f;
}
public void setGender(String gender)
{
if(gender=="male")
is_male=true;
else if(gender=="female")
is_male=false;
else
System.out.println("error");
}
}
/**********************************************
***********************************************/
class EmployeeManagement {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp=new Employee();
int empid = emp.getId();
System.out.println(empid);
emp.setId(1);
emp.setName("adam");
emp.setBasicSalary(3000);
emp.setAllowances(120);
emp.setGender("male");
System.out.println("employee info is: \n----------------------------");
System.out.println("id:"+emp.getId());
System.out.println("name:"+emp.getName());
System.out.println("basic salary :"+emp.getSalary());
System.out.println("allowances:"+emp.getAllowances());
System.out.println("total salary:"+(emp.getSalary()+emp.getAllowances()));
System.out.println("gender :"+emp.getGender());
}
}