JVM Knowledge

A class or interface C is accessible to a class or interface D if and only if either of the following is true:

  • C is public.
  • C and D are members of the same run-time package (§5.3).

A field or method R is accessible to a class or interface D if and only if any of the following is true:

  • R is public.
  • R is protected and is declared in a class C, and D is either a subclass of C or C itself. Furthermore, if R is not static, then the symbolic reference to R must contain a symbolic reference to a class T, such that T is either a subclass of D, a superclass of D, or D itself.
  • R is either protected or has default access (that is, neither public nor protected nor private), and is declared by a class in the same run-time package as D.
  • R is private and is declared in D.

This discussion of access control omits a related restriction on the target of a protected field access or method invocation (the target must be of class D or a subtype of D). That requirement is checked as part of the verification process (§4.10.1.8); it is not part of link-time access control.