Packages and Access Control
The package system in Java does more than just establish separate namespaces for the various
parts of a system. The package system also plays into the access scheme used in the language.
Like most object-oriented languages, Java allows the programmer to declare who can access
what parts (if any) of an object. As befits its C++ heritage, Java allows fields and methods to
be accessed by any part of a class in which those fields and methods are declared. But outside
of an object, access is defined by the access control modifier associated with the field or meth-
od. Fields or methods that are labeled private can be accessed only from within the defining
class. Those that are labeled protected can be accessed either by other parts of the class or
by any class that extends the defining class. Finally, those that are marked public may be ac-
cessed by anyone. This much is familiar to those who come to Java from C++.
But Java has an additional access category connected to the package system. Unless a field or
method is marked as private , that field or method is also accessible to anything that resides
in the same package as the field or method. This is no surprise for fields or methods that have
been labeled as public , since anyone from any package can access such fields. But it is some-
what more surprising for those fields or methods that are marked as protected , since it al-
lows access from methods that are in the package but have no relationship to the defining class
through the type hierarchy. In fact, this introduces a fourth form of access specification, which
is marked by there being no access specification at all. If a field or method is not labeled as
being private , protected , or public , that field or method is said to have package access,
which means that it is accessible by anything in the package, but not by anything else.
This gives a hierarchy of access possibilities for the programmer. At the most restricted are
those fields and methods marked private , which can be accessed only from within the class
in which they occur. Next most restrictive are those with no declared access specification.
These have package access, which makes them available to anything that is in the same pack-
age, but keeps them from the prying eyes of anything in any other package. The next level of
access, protected , loosens the restrictions on package protection to include any classes that
are extensions of the class in which the method or field are defined, no matter where in the set
of packages those extensions are defined. Finally, there are those methods and fields that are
marked as public , which can be accessed from anywhere.
It is important to keep in mind one difference between the hierarchy formed by packages and
the hierarchy formed by classes. The hierarchy formed by classes is inclusive; that is, an ob-
ject that is an instance of a class is an instance of any class that is in the hierarchy above that
class. This means that the protected access specifier opens the access to the field or method to