6.6.1 Determining Accessibility
- Whether a package is accessible is determined by the host system (§7.2).
- If a class or interface type is declared
public, then it may be accessed by any Java code that can access the package in which it is declared. If a class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.
- A member (field or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
- If the member or constructor is declared
public, then access is permitted. All members of interfaces are implicitly public.
- Otherwise, if the member or constructor is declared
protected, then access is permitted only when one of the following is true:
- Access to the member or constructor occurs from within the package containing the class in which the
protected member is declared.
- Access occurs within a subclass of the class in which the
protected member is declared, and the access is correct as described in §6.6.2.
- Otherwise, if the member or constructor is declared
private, then access is permitted only when it occurs from within the class in which it is declared.
- Otherwise, we say there is default access, which is permitted only when the access occurs from within the package in which the type is declared.