Encapsulation and classes
The idea of encapsulation is to create objects that hold a private state and that keep control over it. Keeping control means that other objects may not modify the private state directly. Instead, they have to call specific functions called methods to be able to make those changes. Encapsulation is a core idea of OOP; it helps to ensure that mutations (that is, modifications) cannot be made without control/oversight of the owner of the relevant data. Also, encapsulation helps in hiding complexity.
To support encapsulation, OOP languages have the notion of classes. A class is a logical unit that describes a single concept with all its fields (that is, the properties representing the state) and methods (the operations ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access