The access modifiers public
and private
control access to a class’s variables, methods and properties. (In Chapter 11, we’ll introduce the additional access modifier protected
.) As we stated in Section 10.2, the primary purpose of public
methods and properties is to present to the class’s clients a view of the services the class provides (that is, the class’s public interface). Clients of the class need not be concerned with how the class accomplishes its tasks. For this reason, a class’s private
variables, properties and methods (i.e., the class’s implementation details) are not directly accessible to the class’s clients.
Figure 10.3 demonstrates that private
class members are not directly accessible outside the class. In this app, we use a modified version of class Time1
that declares private
instance variables hour
, minute
and second
, rather than public
properties Hour
, Minute
and Second
. Lines 9–11 attempt to directly access private
instance variables hour
, minute
and second
of Time1
object time
. When this app is compiled, the compiler generates error messages stating that these private
members are not accessible.