8.3 Controlling Access to Members

The access modifiers public and private control access to a class’s variables and methods. In Chapter 9, we’ll introduce the additional access modifier protected. The primary purpose of public methods is to present to the class’s clients a view of the services the class provides (i.e., the class’s public interface). Clients need not be concerned with how the class accomplishes its tasks. For this reason, the class’s private variables and private methods (i.e., its implementation details) are not accessible to its clients.

Figure 8.3 demonstrates that private class members are not accessible outside the class. Lines 9–11 attempt to access the private instance variables hour, minute and second of the Time1 object ...

Get Java™ How To Program (Early Objects), Tenth Edition 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.