3.7. Apply encapsulation principles to a class

[6.5] Apply encapsulation principles to a class

As the heading of this section suggests, we’ll apply the encapsulation principle to a class. A well-encapsulated object doesn’t expose its internal parts to the outside world. It defines a set of methods that enables the users of the class to interact with it.

As an example from the real world, you can compare a bank to a well-encapsulated class. A bank doesn’t expose its internal parts—for example, its vaults and bank accounts—to the outside world, just as a well-encapsulated class in Java shouldn’t expose the variables that it uses to store the state of an object outside that object. The way a bank defines a set of procedures (such ...

Get OCA Java SE 8 Programmer I Certification Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.