-
Notifications
You must be signed in to change notification settings - Fork 7
OOPS
Abhishek Khare edited this page Jan 9, 2017
·
7 revisions
- The beauty of encapsulation is the power of changing things without affecting its users.
- In Java, you achieve encapsulation by hiding details using the accessibility modifiers (public, protected, private, plus no modifier which implies package private). With these levels of accessibility you control the level of encapsulation, the less restrictive the level, the more expensive change is when it happens and the more coupled the class is with other dependent classes.
- The idea is to provide a public interface through which you gain access to this data. You can later change the internal representation of the data without compromising the public interface of the class. On the contrary, by exposing the data itself, you compromise encapsulation, and therefore, the capacity of changing the way you manipulate the data without affecting its users.
- There are several reasons why you might want to encapsulate access to your fields. For example:
- You can limit the values that can be stored in a field