Computer Applications
What are the advantages of encapsulation?
Encapsulation & Inheritance in Java
4 Likes
Answer
The advantages of encapsulation are as follows:
- Data Hiding — Encapsulation enables the programmer to hide desired data by making it private and give access only to the desired classes / methods. The programmer can hide how variables and data are stored.
- Implementation Logic are Hidden — User only knows that to update a data member's value, call its setter method and to read a data member's value, call its getter method but what these setter and getter methods are doing, is purely hidden from them.
- Flexibility — Since implementation details are hidden, it is easier to change the inner logic.
- More control in programmer's hand — With Java encapsulation, a programmer has full control over which values are allowed via setter and getter methods. This allows making the data read-only or write-only as per requirements. It lead to more flexibility and more control in programmer's hand.
Answered By
3 Likes