Part 1 of 7 · 1 min read
Why this page comes second
In this module you will
- Explain encapsulation as information hiding, and why it decouples classes.
- Apply the rule of the lowest possible access level.
- Choose accessors over public fields, and know when to leave out setters.
- Keep a class's rules (invariants) true with checks in constructors and methods.
- Follow the five rules that make a class immutable.
Module 1 gave objects data and behaviour. Encapsulation decides who may touch that data. Most design rules in Level 2 come back to it. The main source is "Effective Java" (EJ) Items 15 to 17, with "Design Patterns" (GoF) on how inheritance can break it. Code was compiled and run before it was written here. Numbers marked "made up" only show the idea.