options described in the preceding section to specify the path mypack.
)
As explained, AccountBalance is now part of the package mypack.
This means that it cannot be executed by itself. That is, you cannot use this
command line:
java AccountBalance
AccountBalance must be qualified with its package name.
Packages and Member Access
In the preceding chapters, you learned about various aspects of Java’s
access control mechanism and its access modifiers. For example, you
already know that access to a private member of a class is granted only to
other members of that class. Packages add another dimension to access
control. As you will see, Java provides many levels of protection to allow
fine-grained control over the visibility of variables and methods within
classes, subclasses, and packages.
Classes and packages are both means of encapsulating and containing
the name space and scope of variables and methods. Packages act as
containers for classes and other subordinate packages. Classes act as
containers for data and code. The class is Java’s smallest unit of
abstraction. As it relates to the interplay between classes and packages,
Java addresses four categories of visibility for class members:
• Subclasses in the same package
• Non-subclasses in the same package
• Subclasses in different packages
• Classes that are neither in the same package nor subclasses
The three access modifiers, private, public, and protected, provide a
variety of ways to produce the many levels of access required by these
categories. Table 9-1 sums up the interactions.
Table 9-1 Class Member Access
While Java’s access control mechanism may seem complicated, we can
simplify it as follows. Anything declared public can be accessed from
different classes and different packages. Anything declared private cannot
be seen outside of its class. When a member does not have an explicit
access specification, it is visible to subclasses as well as to other classes in
the same package. This is the default access. If you want to allow an
element to be seen outside your current package, but only to classes that
subclass your class directly, then declare that element protected.
Table 9-1 applies only to members of classes. A non-nested class has
only two possible access levels: default and public. When a class is
declared as public, it is accessible outside its package. If a class has
default access, then it can only be accessed by other code within its same
package. When a class is public, it must be the only public class declared
in the file, and the file must have the same name as the class.
Note The modules feature can also affect accessibility. Modules are
described in Chapter 16.
An Access Example
The following example shows all combinations of the access control
modifiers. This example has two packages and five classes. Remember
that the classes for the two different packages need to be stored in
directories named after their respective packages—in this case, p1 and p2.
The source for the first package defines three classes: Protection,
Derived, and SamePackage. The first class defines four int variables in
each of the legal protection modes. The variable n is declared with the
default protection, n_pri is private, n_pro is protected, and n_pub is
public.
Each subsequent class in this example will try to access the variables in
an instance of this class. The lines that will not compile due to access
restrictions are commented out. Before each of these lines is a comment
listing the places from which this level of protection would allow access.
The second class, Derived, is a subclass of Protection in the same
package, p1. This grants Derived access to every variable in Protection
except for n_pri, the private one. The third class, SamePackage, is not a
subclass of Protection, but is in the same package and also has access to
all but n_pri.
This is file [Link]:
This is file [Link]:
This is file [Link]:
Following is the source code for the other package, p2. The two classes
defined in p2 cover the other two conditions that are affected by access
control. The first class, Protection2, is a subclass of [Link]. This
grants access to all of [Link]’s variables except for n_pri (because
it is private) and n, the variable declared with the default protection.
Remember, the default only allows access from within the class or the
package, not extra-package subclasses. Finally, the class OtherPackage
has access to only one variable, n_pub, which was declared public.
This is file [Link]:
This is file [Link]: