Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule is wrapped with different medicines. Java Encapsulation reduces the code redundancy easily.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
To achieve encapsulation in Java −
- Declare the variables of a class as private.
- Provide public setter and getter methods to modify and view the values of the variables.