This is fun so let me try to summarize it. see JLS#6.6.1
protected
can qualify a constructor or a member of a class.
"member" includes field/method (static/instance), nested class/interface (static/inner)
class A {
protected int f
protected void m(){}
protected class X{}
protected interface Y{}
First, to access a protected constructor/member, the enclosing class (e.g. A
) must be accessible. Assume that's the case, then --
--Inside the package --
A protected constructor or member is accessible anywhere within the same package.
--Outside the package --
A protected constructor is only accessible within subclass constructors, either as a super()
call, or as an anonymous class instantiation.
A protected static field/method, nested class/interface is accessible anywhere within subclass bodies.
A protected instance field/method is more complex --
- protected "m" is defined in a class A
obj.m
is accessed in class B (outside A's package)
obj
's type is C
The access obj.m
is granted only if B is subclass of A, and C is subclass of B or C is B.
super.m
is always allowed; however, it's unclear how JLS frames the issue. It seems that the access should be treated the same as this.m
, therefore access is allowed.