You are on page 1of 1

Java provides four access modifiers.

 public: Accessible to everyone.
 private: Accessible only within the class.
 protected: Accessible to the containing package and the subclasses.
 default: Accessible within the package.

Why is Encapsulation used in Java?

Encapsulation in Java is mostly useful to hide the data. Or in other words, to decide about the access
given to data as to who can access it, and who cannot.

What are the advantages of Encapsulation?

1. The encapsulated code is more flexible and easy to change with new requirements.
2. It prevents the other classes to access the private fields.
3. Encapsulation allows modifying implemented the code without breaking others code who have
implemented the code.
4. It keeps the data and codes safe from external inheritance. Thus, Encapsulation helps to achieve
security.
5. It improves the maintainability of the application.

You might also like