Encapsulation (or data hiding) refers to not having outside methods access or change the data within a class, using access modifiers.
Using encapsulation reduces any work you have to do to make minor adjustments if things within your class definition may change. Imagine we change the type of a variable and have outside methods that rely on it, and we had to go around and figure out what to change to match this change.
This means that:
- Code outside the class doesn’t know how a data member is defined (and it doesn’t need to).
- Allows us to change implementation of members without affecting outside code (so long as the public interface stays the same). What are the implications of this?
- Limits required changes of code.
- Reduces number of errors.
- Increases productivity.