Previous | Table of Contents | Next |
14.3.8.1. Visibility Modifiers
In most of our examples so far, youve probably noticed the public keyword being used. When applied to a class, it means that the class is visible everywhere. When applied to a method or variable, it means that the method or variable is visible everywhere.
To hide variables (or methods, for that matter), you just have to declare them private:
public class Laundromat { // People can use this class. private Laundry[] dirty; // They cant see this internal variable, public void wash() { ... } // but they can use these public methods public void dry() { ... } // to manipulate the internal variable. }
A private field of a class is visible only in methods defined within that class. (Or, in Java 1.1, in classes defined within the class.) Similarly, a private method may only be invoked by methods within the class (or methods of classes within the class). Private members are not visible within subclasses, and are not inherited by subclasses as other members are.23 Of course, non-private methods that invoke private methods internally are inherited and may be invoked by subclasses.
23Every object does, of course, have its own copy of all fields of all superclasses, including the private fields. The methods defined by the object can never refer to or use the private fields of superclasses, however, and so we say that those fields are not inherited.
Besides public and private, Java has two other visibility levels: protected and the default visibility level, package visibility, which applies if none of the public, private, and protected keywords are used.
A protected member of a class is visible within the class where it is defined, of course, and within all subclasses of the class, and also within all classes that are in the same package as that class. You should use protected visibility when you want to hide fields and methods from code that uses your class, but want those fields and methods to be fully accessible to code that extends your class.
The default package visibility is more restrictive than protected, but less restrictive than private. If a class member is not declared with any of the public, private, or protected keywords, then it is visible only within the class that defines it and within classes that are part of the same package. It is not visible to subclasses unless those subclasses are part of the same package.
A note about packages: A package is a group of related and possibly cooperating classes. All non-private members of all classes in the package are visible to all other classes in the package. This is okay because the classes are assumed to know about, and trust, each other.24 The only time difficulty arises is when you write programs without a package statement. These classes are thrown into a default package with other package-less classes, and all their non-private members are visible throughout the package. (The default package usually consists of all classes in the current working directory.)
24If youre a C++ programmer, you can say that classes within the same package are friendly to each other.
There is an important point to make about subclass access to protected members. A subclass inherits the protected members of its superclass, but it can only access those members through instances of itself, not directly in instances of the superclass. Suppose, for example, that A, B, and C are public classes, each defined in a different package, and that a, b, and c are instances of those classes. Let B be a subclass of A, and C be a subclass of B. Now, if A has a protected field x, then the class B inherits that field, and its methods can use this.x, b.x, and c.x. But it cannot access a.x. Similarly, if A has a protected method f(), then the methods of class B can invoke this.f(), b.f(), and c.f(), but they cannot invoke a.f().
Table 14.4 shows the circumstances under which class members of the various visibility types are accessible to other classes.
MemberVisibility | ||||
---|---|---|---|---|
Accessible To | public | protected | package | private |
Same class | Yes | Yes | Yes | Yes |
Class in same package | Yes | Yes | Yes | No |
Subclass in different package | Yes | Yes | No | No |
Non-subclass, different package | Yes | No | No | No |
The details of member visibility in Java can become quite confusing. Here are some simple rules of thumb for using visibility modifiers:
Note that you cant take advantage of package visibility unless you use the package statement to group your related classes into packages.
Previous | Table of Contents | Next |