Java - Object-Oriented Programming (OOPS) Concept - Encapsulation Tutorial
As the word encapsulation says something sensitive that is to hide. It is wrapping the data (variables) and code (methods) together into a single unit.
In encapsulation, a variable in one class cannot be accessed by the other class. But it can be accessed through the method of the current class. That is why encapsulation is something use to hide data.
To achieve encapsulation,
- We have to declare the variable as private in one class.
- And also we have to use the getter and setter method in one class which can be used by other classes. Which will hide the variable from users.