I'll Be Backend
Principles

Encapsulation: Public Fields to Guarded State

Encapsulation means an object keeps its fields private and offers methods for the changes it allows, so its state can only change in one place. These lessons take a gift card whose public balance is written by checkout, refunds and support, and move every change into the card, so an audit history becomes one line in each of its methods.

Pick your language

When to use it

Use it when

Several classes change the same value, and a rule must hold every time it changes.

Skip it when

The class only carries data with no rule, like a DTO read by one view. Accessors there add lines and guard nothing.

Spot it by

The same field assigned in many classes, or a check copied before each write.