http://www.technicalpage.net/search/label/SQL

>> Encapsulation in Java


Encapsulation - Setter / Getter - Data hiding:
Encapsulation is the way of hiding the variable or data by making them private. The variable or data are made private so that other class or code can not access them .  The outer class or code can access them creating  public methods  inside the owner class. The public methods act as setter and getter of the private variable.  Encapsulation is also called data hiding. It is used for making data secure , you can make it visible to outer class only if it is necessary.


public class OwnerClass {
private String name = "David";
private int serialNo = 10;
private float income = 2000.55f;
private char status ;
private int empNo ;
//These variables are accessible from within this class.
//For example:
private void internalTest() {
       System.out.println("With in the class , employee's name , serialNo, income are "+name+" , "+serialNo+" , "+income);
}

//Below main method is created to show that above variables are accessible within owner class.
public static void main (String[] args) {
       EncapsulationCLS  obj = new EncapsulationCLS();
       obj.internalTest();
}
}

Output:
With in the class , employee's name , serialNo, income are David , 10 , 2000.55


//however these variable are not accessiable from outside the owner class.
//To make these accessible from outside the class, we use setter/getter methods as below:


1.
//To get the private value , if the private variable is initialized already and if you want to use the same value, then no need of getter and setter.
public class OwnerClass {
private String name = "David";
private int serialNo = 10;
private float income = 2000.55f;
private char status ;
private int empNo ;

public String name() {
    return name;
 }

public int  serialNo() {
    return serialNo;
 }

public float  income() {
    return income;
 }
 }

public class OutsideClass {
       public static void main(String[] args) {
             
              OwnerClass obj = new OwnerClass();
              obj.name();
              obj.income();
              obj.serialNo();
             
              System.out.println("Employee Name is : "+obj.name());
              System.out.println("Employee Income is : "+obj.income());
              System.out.println("Employee Serial No is : "+obj.serialNo());

       }
}

Output:
Employee Name is : David
Employee Income is : 2000.55
Employee Serial No is : 10


2.
public class OwnerClass {
private String name = "David";
private int serialNo = 10;
private float income = 2000.55f;
private char status ;
private int empNo ;

public String name() { //Returning same original value
    return name;
 }

public int  serialNo(int serialNo) { //Returning new entered value
    return serialNo;
 }

public float  income(float income) { //Returning same original value even though we enter different value while calling.
    return this.income;  
 }

}


public class OutsideClass {
       public static void main(String[] args) {

              OwnerClass obj = new OwnerClass();

              obj.name();
              obj.serialNo(20);
              obj.income(1500.67f);

              System.out.println("Employee Name is : "+obj.name());
              System.out.println("Employee Serial No is : "+obj.serialNo(20));
              System.out.println("Employee Income is : "+obj.income(1500.67f));
   }
}


Output:
Employee Name is : David
Employee Serial No is : 20  //New Value
Employee Income is : 2000.55 //Returning same value even though we entered different value while calling.


3.
if you are using the private variable which are not initialized or if you are using the initialized private variable with different variable value.

public class OwnerClass {
private String name = "David";
private int serialNo = 10;
private float income = 2000.55f;
private char status ;
private int empNo ;

public String getName(String name) {  //initialized but using different value
    return name;
 }

public char  getStatus(char status ) { //not initialized
    return status;
 }

public int  getEmpNo(int empNo) { //not initialized
    return empNo;
 }


public class OutsideClass {
       public static void main(String[] args) {
             
              OwnerClass obj = new OwnerClass();
              obj.getName("Rahul");
              obj.getStatus('Y');
              obj.getEmpNo(101);
             
              System.out.println("Employee Name is : "+obj.getName("Rahul"));
              System.out.println("Employee Status is : "+obj.getStatus('Y'));
              System.out.println("Employee Employee No. is : "+obj.getEmpNo(101));

       }
}

Output:
Employee Name is : Rahul
Employee Status is : Y
Employee Employee No. is : 101


 4.
//Above code can also be written using getter and setter as below:
public class OwnerClass {
private String name = "David";
private int serialNo = 10;
private float income = 2000.55f;
private char status ;
private int empNo ;

public String getName() {
    return name;
 }

public int getSerialNo() { //We will use same original value , not use new value of serialNo
    return serialNo;
 }

public char  getStatus() {
    return status;
 }

public int  getEmpNo() {
    return empNo;
 }

public void setName(String NewName) {
    name = NewName;
 }

//We are using original value of serialNo, so no need of below statement , we can directly call getSerialNo(), For learning purpose only it is displayed here.
public void setSerialNo() {
       serialNo = serialNo;
 }

public void setStatus(char NewStatus) {
    status = NewStatus;
 }

public void setEmpNo(int NewEmpNo) {
       empNo = NewEmpNo;
 }

}


public class OutsideClass {

       public static void main(String[] args) {
             
              OwnerClass obj = new OwnerClass();
              obj.setName("Rahul");
              obj.setSerialNo(); //We can directly call obj.getSerialNo();
              obj.setStatus('Y');
              obj.setEmpNo(101);
             
             
              System.out.println("Employee Name is : "+obj.getName());
              System.out.println("Employee Serial No. is : "+obj.getSerialNo());
              System.out.println("Employee Status is : "+obj.getStatus());
              System.out.println("Employee Employee No. is : "+obj.getEmpNo());

       }
}

Output:
Employee Name is : Rahul
Employee Serial No. is : 10  //Same original value
Employee Status is : Y
Employee Employee No. is : 101


No comments:

Post a Comment