Encapsulation in Java

19:46:00

Encapsulation in Java

Encapsulation is a process of wrapping of data and methods in a single unit is called encapsulation. Encapsulation is achieved in java language by class concept.

Combining of state and behavior in a single container is known as encapsulation. In java language encapsulation can be achieve using class keyword, state represents declaration of variables on attributes and behavior represents operations in terms of method.
Advantage of Encapsulation
The main advantage of using of encapsulation is to secure the data from other methods, when we make a data private then these data only use within the class, but these data not accessible outside the class.
Real life example of Encapsulation
The common example of encapsulation is capsule. In capsule all medicine are encapsulated in side capsul
Benefits of encapsulation
  1.          Provides abstraction between an object and its clients.
  2.          Protects an object from unwanted access by clients.


      Example: A bank application forbids (restrict) a client to change an Account's balance.
Let's see the Example of Encapsulation in java
public class EncapsulationDemo{
    private int ssn;
    private String empName;
    private int empAge;

    //Getter and Setter methods
    public int getEmpSSN(){
        return ssn;
    }

    public String getEmpName(){
        return empName;
    }

    public int getEmpAge(){
        return empAge;
    }

    public void setEmpAge(int newValue){
        empAge = newValue;
    }

    public void setEmpName(String newValue){
        empName = newValue;
    }

    public void setEmpSSN(int newValue){
        ssn = newValue;
    }
}
public class EncapsTest{
    public static void main(String args[]){
         EncapsulationDemo obj = new EncapsulationDemo();
         obj.setEmpName("Mario");
         obj.setEmpAge(32);
         obj.setEmpSSN(112233);
         System.out.println("Employee Name: " + obj.getEmpName());
         System.out.println("Employee SSN: " + obj.getEmpSSN());
         System.out.println("Employee Age: " + obj.getEmpAge());
    } 
}


Output:


Employee Name: Mario
Employee SSN: 112233
Employee Age: 32



 Advantages of encapsulation:

1.    It improves maintainability and flexibility and re-usability: for e.g. In the above code the implementation code of void setEmpName(String name) and  String getEmpName() can be changed at any point of time. Since the implementation is purely hidden for outside classes they would still be accessing the private field empName using the same methods  (setEmpName(String name) and  getEmpName()). Hence the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-usability of the underlying class.
2.   The fields can be made read-only (If we don’t define setter methods in the class) or write-only (If we don’t define the getter methods in the class). For e.g. If we have a field(or variable) which doesn’t need to change at any cost then we simply define the variable as private and instead of set and get both we just need to define the get method for that variable. Since the set method is not present there is no way an outside class can modify the value of that field.
3.   User would not be knowing what is going on behind the scene. They would only be knowing that to update a field call set method and to read a field call get method but what these set and get methods are doing is purely hidden from them.


Encapsulation is also known as “data Hiding”.

1.Objects encapsulate data and implementation details. To the outside world, an object is a black box that exhibits a certain behavior.
2.The behavior of this object is what which is useful for the external world or other objects.
3.An object exposes its behavior by means of public methods or functions.
4.The set of functions an object exposes to other objects or external world acts as the interface of the object.

You Might Also Like

0 comments

Popular Posts

Like us on Facebook