Java Reference
In-Depth Information
access control to members (whether fields, methods, types) is applied explicitly by
field access expressions, method invocation expressions, and qualified class instance
creation expressions. (Note that access to a field may also be denoted by a qualified
name occuring as a postfix expression.)
Note that qualified names, field access expressions, method invocation expressions,
and qualified class instance creation expressions are syntactically similar in that a “ .
token appears, preceded by some indication of a package, type, or expression having
a type, and followed by an Identifier that names a member of the package or type. (A
new token intercedes between the . and the Identifier in a qualified class instance cre-
ation expression.)
Many statements and expressions allow the use of types rather than type names. For
example, a class declaration may use a parameterized type (§ 4.5 ) to denote a super-
class. Because a parameterized type is not a qualified type name, it is necessary for
the class declaration to explicitly perform access control for the denoted superclass.
Consequently, most of the statements and expressions that provide contexts in § 6.5.1
to classify a TypeName must also perform their own access control checks.
Beyond access to members of a package or reference type, there is the matter of access
to constructors of a reference type. Access control must be checked when a construct-
or is invoked explicitly or implicitly. Consequently, access control is checked by an
explicit constructor invocation statement (§ 8.8.7.1 ) and by a class instance creation
expression (§ 15.9.3 ). These “manual” checks are necessary because § 6.5.1 ignores
explicit constructor invocation statements (because they reference constructor names
indirectly) and is unaware of the distinction between the class type denoted by an un-
qualified class instance creation expression and a constructor of that class type. Also,
constructors do not have qualified names, so we cannot rely on access control being
checked during classification of qualified type names.
Accessibility affects inheritance of class members (§ 8.2 ) , including hiding and meth-
od overriding (§ 8.4.8.1 ) .
6.6.1. Determining Accessibility
• A package is always accessible.
• If a class or interface type is declared public , then it may be accessed by any code,
provided that the compilation unit (§ 7.3 ) in which it is declared is observable.
If a top level class or interface type is not declared public , then it may be accessed
only from within the package in which it is declared.
• An array type is accessible if and only if its element type is accessible.
Search WWH ::




Custom Search