Encapsulation is a mechanism through which we hide the data members and access those data members through public method known as getters and setter.
Or
Or
The Process of binding the data with related methods known as Encapsulation .
private String name;
private String empCode;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
}
public class EncapsulationExample {
public static void main(String[] args) {
Employee e = new Employee();
e.setName("abc");
e.setEmpCode("EMP001");
System.out.println("EMP NAME\t:"+e.getName());
System.out.println("EMP CODE\t:"+e.getEmpCode());
}
}

No comments:
Post a Comment