Encapsulation in Java
- Encapsulation:
public class, implement class from Serializable, declare variables as private, one no parameter constructor, one parameterized constructor, one pair of setter and getter methods, overriding equals(), hashCode() and toString() methods.
Hiding data members / variables of a class and expose them through setter and getter methods. For that we need to declare class instance variables as private.
Emp.txt / table:emp
1&ABC&10000.00&Designer
2&XYZ&20000.00&Developer
3&LMN&30000.00&Tester
4&PQR&40000.00&Analyst
- To access data from the .txt file into Java Bean, in the bean the variables are declared as private, to the required member variables setter and getter methods are provided (setEid(int eid), getEname(), getSal(), getDesig()) and also one more method implemented in the class named retrieveEmp(), once after setting eid through setter method in the client program we must call retrieveEmp() method which retrieves requested emp details from file/db/network/cloud and through getter methods we can access ename, sal & desig.
- The bean is encapsulated in such a way that others cannot modify ename, sal, and desig.
// Emp.java
public class Emp {
int eid;
String ename;
double sal;
String desig;
}
// EmpClient.java
public class EmpClient {
public static void main(String rags[]) {
Emp e=new Emp();
e.eid=1;
e.ename="ABC";
e.sal=100000.00;
e.desig="Developer";
System.out.println(e.ename);
}
}
- If variables are not declared as private then classes outside of our class can instantiate Emp bean and its variables can be directly accessed and modified.
- There is no security to member variables of our class.
- If variables are declared as private after instantiating Emp object Emp e=new Emp();, if outside class tries to access using e.eid=1; or e.ename="ABC"; then compiler immediately raises error saying private variables cannot be accessed outside the class.
- Hence we provide required setter() and getter() methods.
- only getter methods are provided to ename, sal and desig. Then those variables cannot be modified. Cannot be modified by even authorized members of our org.
- If modification privileges want to be provided only to authorized members of our org, then in each setEname() along with String we must also ask for auth pass, similarly in setSal() and setDesig() also.
- Unless if the member is authorized they cannot modify ename, sal, desig.
// Emp.java
import java.io.*;
public class Emp {
private int eid;
private String ename;
private double sal;
private String desig;
public Emp() {
}
// pair of public mutator/setter & accessor/getter methods we need to write for each private variable
public void setEid(int eid) {
this.eid=eid;
}
public int getEid() {
return eid;
}
public void retrieveEmp() {
try {
java.io.RandomAccessFile raf=new java.io.RandomAccessFile("emp.txt","r");
String line=raf.readLine();
while(line!=null) {
StringTokenizer st=new StringTokenizer(line, "&");
int tempeid=Integer.parseInt(st.nextToken());
if(this.eid==tempeid) {
ename=st.nextToken();
sal=Double.parseDouble(st.nextToken());
desig=st.nextToken();
break;
} // if()
line=raf.readLine();
} // while()
} // try
catch(Exception e) {}
} // retrieveEmp()
/* protected void setEname(String ename) {
this.ename=ename;
}*/
public String getEname() {
return ename;
}
/* protected void setSal(double sal) {
this.sal=sal;
}*/
public double getSal() {
return sal;
}
/* protected void setDesig(String desig) {
this.desig=desig;
}*/
public String getDesig() {
return desig;
}
}// class
// EmpClient.java
class EmpClient {
public static void main(String rags[]) {
Emp e1=new Emp();
e1.setEid(3);
e1.retrieveEmp();
// e1.ename="MNO";
// e1.setEname("MNO");
System.out.println(e1.getEname());
System.out.println(e1.getSal());
System.out.println(e1.getDesig());
}
}
0 Comments