Encapsulation means hiding an object's internal state and exposing it only through methods you control. In practice you make fields private and provide methods such as getters and setters, or better, meaningful operations like deposit(amount). Because callers cannot reach the fields directly, the class can validate changes, keep its invariants and change its internal representation later without breaking other code.
Keep reading for free
Create a free StudyToCert account to read the rest of this lesson: 6 more sections, 4 key terms, a real-world example, an exam tip and self-check questions. Every lesson, lab and practice test is free with an account.