Eclipse shows an error when trying to use protected constructor in a subclass located in other package [duplicate]

老子叫甜甜 提交于 2019-12-01 09:24:44

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.

See Java Language Specification:

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

Your class A2 is not responsible for the implementation of A in the new A() call.

Meaning, it is not responsible for the implementation of an instance of A, but it is responsible for the implementation of an instance of A2.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!