- Binding Data(variable) and code together(Method)
- It restrict direct Access to Data
- Controls access through Getter and Setter methods
=> Why Encapsulation
- Prevents data from unauthorised access
- Ensures security
- maintainability
=> Super class
public class Joyalukkas
{
private int price=1000;
public int getprice()
{
return this.price;
}
public void setprice(int price){
this.price=price;
}
}
=> Sub class
public class Buyer
{
public static void main (String[]args)
{
Joyalukkas joyalukkas=new Joyalukkas();
System.out.println(joyalukkas.getprice());
}
}
OUTPUT:
=> Getter Method
- Getter method is a public method used to read the value of private variable from outside of the class
=> Setter Method
- Setter method is a public method used to modify the value of private variable from outside of the class

Top comments (0)