Java 1
Java 1
public: Accessible to everyone.
private: Accessible only within the class.
protected: Accessible to the containing package and the subclasses.
default: Accessible within the package.
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.
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.