Java Reference
In-Depth Information
18.3. Package Access
You have two options when declaring accessibility of top-level classes and
interfaces within a package: package and public. A public class or inter-
face is accessible to code outside that package. Types that are not public
have package scope: They are available to all other code in the same
package, but they are hidden outside the package and even from code
in subpackages. Declare as public only those types needed by program-
mers using your package, hiding types that are package implementation
details. This technique gives you flexibility when you want to change the
implementationprogrammers cannot rely on implementation types they
cannot access, and that leaves you free to change them.
A class member that is not declared public , protected , or private can be
used by any code within the package but is hidden outside the package.
In other words, the default access for an identifier is "package" except
for members of interfaces, which are implicitly public.
Fields or methods not declared private in a package are available to all
other code in that package. Thus, classes within the same package are
considered "friendly" or "trusted." This allows you to define application
frameworks that are a combination of predefined code and placeholder
code that is designed to be overridden by subclasses of the framework
classes. The predefined code can use package access to invoke code in-
tended for use by the cooperating package classes while making that
code inaccessible to any external users of the package. However, sub-
packages are not trusted in enclosing packages or vice versa. For ex-
ample, package identifiers in package dit are not available to code in
package dit.dat , or vice versa.
Every type has therefore three different contracts that it defines:
The public contract, defining the primary functionality of the type.
The protected contract, defining the functionality available to sub-
types for specialization purposes.
 
Search WWH ::




Custom Search