You are currently viewing Java Encapsulation

Java Encapsulation

  • Post author:
  • Post category:Java / Java OOP
  • Post last modified:July 7, 2021
  • Reading time:1 mins read

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 −

  1. Declare the variables of a class as private.
  2. Provide public setter and getter methods to modify and view the values of the variables.