Skip to content

Encapsulation: private fields, getters, setters

Handout

Protect your data

  • Encapsulation means hiding an object's data and only changing it through methods.
  • This keeps the data safe. Other code cannot put a bad value into it.
  • The main tools are the words private and public.

A private balance field is reached only through getBalance and setBalance

Handout

Log in or create account

IGCSE & A-Level