Encapsulation in Java

In a general language, encapsulation can be termed as “the action of enclosing something in” and in a programming language, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination. Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safes from outside interference and misuse.

One way to think about encapsulation in Java is a protective wrapper that prevents the code and the data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and the data inside the wrapper is tightly controlled through a well-defined interface.

In Java, the basis of encapsulation is the class. Below is the java program that demonstrates implementation of encapsulation:

import java.util.Scanner;
class Rectangle{
    private int width;     //private variables which can only be accessed by public methods of class
    private int height;

public Rectangle(){
}

public Rectangle(int width,int height){
    this.width = width;
    this.height = height;
}
public int getWidth(){          //get method to access private variable width
    return width;
}
public void setWidth(int a){    //set method to access private variable width
    width=a;
}
public int getHeight(){         //get method to access private variable height
    return height;
}
public void setHeight(int a){   //set method to access private variable height
    height=a;
}
public void acceptData(){
    Scanner in = new Scanner(System.in);
    System.out.print("enter width:");
    width = in.nextInt();
    System.out.print("enter height:");
    height = in.nextInt();
}
public void showData(){
    System.out.print("width:"+width);
    System.out.print("height:"+height);
}
public int getArea(){
    return (width*height);
}
public int getPerimeter(){
    return (2*(width+height));
}
public static void main(String args[]){
    Rectangle r = new Rectangle();
    Rectangle r1 = new Rectangle(15,20);
    r.acceptData();
    r.showData();
    System.out.print("Area:"+r.getArea());
  }
}

In the above program, one can understand that the class Rectangle is encapsulated as the variables are declared as private. The “getters and setters” methods are set as public because they are used to access the private variables declared in the code. They are used because the private variables can’t be accessed directly due to encapsulation.

What are advantages of encapsulation?

  • Encapsulation allows you to change one part of code without affecting the other part
  • It allows you to control who can access what
  • If considering the perspective of testing, encapsulation makes unit testing easy
  • Encapsulation also helps in improving the code reusability
  • Data hiding is also an important feature of it in which one can hide important implementation details and just show rest of it
  • Encapsulation also help us to make a flexible code which can easily be changed or maintained

In spiteĀ of so many advantages of it, encapsulation also has a disadvantage, which is you can’t access private data outside class.