One of the most important aspects of object-oriented design is data hiding, or encapsulation. By treating an object in some respects as a “black box” and ignoring the details of its implementation, we can write more resilient, simpler code with components that can be easily reused.
By default, the variables and methods of a class are accessible to members of the class itself and to other classes in the same package. To borrow from C++ terminology, classes in the same package are friendly. We’ll call this the default level of visibility. As you’ll see as we go on, the default visibility lies in the middle of the range of restrictiveness that can be specified.
The modifiers public
and private
, on the other hand, define the extremes. As we mentioned earlier, methods and variables declared as private
are accessible only within their class. At the other end of the spectrum, members declared as public
are accessible from any class in any package, provided the class itself can be seen. [The class that contains the methods must also be public
to be seen outside of its package, as we discussed previously.] The public
members of a class should define its most general functionality—what the black box is supposed to do.
Figure 6-7 illustrates the four simplest levels of visibility, continuing the example from the previous section. Public members in TextArea
are accessible from anywhere. Private members are not visible from outside the class. The default visibility allows access by other classes in the package.
Figure 6-7. Private, default, protected, and public visibility
The protected
modifier allows special access permissions for subclasses. Contrary to how it might sound, protected
is slightly less restrictive than the default level of accessibility. In addition to the default access afforded classes in the same package, protected
members are visible to subclasses of the class, even if they are defined in a different package. If you are a C++ programmer used to more restrictive meanings, this may rub you the wrong way.[]
Table 6-1 summarizes the levels of visibility available in Java; it runs generally from most to least restrictive. Methods and variables are always visible within a declaring class itself, so the table doesn’t address that scope.
Table 6-1. Visibility modifiers
Modifier
Visibility outside the class
private
None
No modifier [default]
Classes in the package
protected
Classes in package and subclasses inside or outside the package
public
All classes
Subclasses and Visibility
Subclasses add two important [but unrelated] complications to the topic of visibility. First, when you override methods in a subclass, the overriding method must be at least as visible as the overridden method. While it is possible to take a private
method and override it with a public
method in a subclass, the reverse is not possible; you can’t override a public
method with a private
method. This restriction makes sense if you recall that subtypes have to be usable as instances of their supertype [e.g., a private
7 is a subclass of private
8 and, therefore, must be usable as an private
8]. If we could override a method with a less visible method, we would have a problem: our private
7 might not be able to do all the things an private
8 can. However, we can reduce the visibility of a variable. In this case, the variable acts like any other shadowed variable; the two variables are distinct and can have separate visibilities in different classes.
The next complication is a bit harder to follow: the protected
variables of a class are visible to its subclasses, but only through objects of the subclass’s type or its subtypes. In other words, a subclass can see a protected
variable of its superclass as an inherited variable, but it can’t access that same variable via a reference to the superclass itself. This statement could be confusing because it might not be obvious that visibility modifiers don’t restrict access between instances of the same class in the same way that they restrict access between instances of different classes. Two instances of the same class can access all of each other’s members, including private ones, as long as they refer to each other as the correct type. Said another way: two instances of private
4 can access all of each other’s variables and methods [including private ones], but a private
4 can’t access a protected member in an instance of private
8 unless the compiler can prove that the private
8 is a private
4. That is, private
4s have the special privileges of being an private
8 only with respect to other private
4s, not just any private
8. If you find this hard to follow, don’t worry too much. If you run into this as a problem in the real world, you are probably trying to do something trickier than you should.
Interfaces and Visibility
Interfaces behave like classes within packages. An interface can be declared public
to make it visible outside its package. Under the default visibility, an interface is visible only inside its package. Like classes, only one public
interface can be declared in a compilation unit [file].