为什么在不相关的类中可以访问受保护的方法?

iam*_*ind 3 java eclipse compilation protected access-specifier

我在下面用Eclipse ide编写的代码:

public interface X
{
  final public static int SOME_CONST = 0;
}
public class Handle implements X
{
  protected void methodHandle () { }
 //...
}

public class User implements X
{
  Handle handle = new Handle();
  private void methodUser ()
  {
    Y y = new Y()  // anonymous inner class
    {
      public void methodY ()
      {
        handle.methodHandle (); // <--- why this is NOT giving error ?
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

即使Handle.methodHandle ()protected,它仍然可以从匿名内部class方法的内部方法调用?它为什么会发生,我错过了什么?Handle和之间的唯一关系User是它们是implement相同的X.

wja*_*ans 6

如果两个类都在同一个包中,则可以调用受保护的方法.

有关详细信息,请参阅