Why can a “private” method be accessed from a different instance?

雨燕双飞 提交于 2019-11-28 20:47:49

Yes, the main method belongs to class A, but it is not accessing the private method from inside the current object in the context of the "this" reference.

That doesn't matter. That's just not the model of accessibility that Java uses. What's important is the class in which the code is written, not whether it's accessing members in the same object.

This is very useful, as otherwise (for example) it would be impossible to implement an equals method using private members of both classes.

This may not be how you would have chosen to specify the language, but it is how Java is specified. See JLS 6.6.1 for details of accessibility. In particular:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

Note that protected access is slightly odd here - a protected instance member of class SuperClass can only be accessed within code in SubClass via an expression of either SubClass or a further subclass. But it still doesn't have to be "the same" object.

private modifer

Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

private means "private to the class". Not "private to the instance".

That's what allows implementing things like static factory methods calling private constructors, or equals() or compareTo() methods comparing private fields of objects of the same class, or copy constructors, etc.

Private members of enclosing classes are also accessible from inner classes of this enclosing class, and vice-versa.

After the technically correct answers here's my two cents why I think it is quite reasonable to implement private that way:

As I see it the main reason that private methods and attributes exists is "implementation hiding". You are declaring "Don't use this method from outside my class(!), since it might change or disapear anytime I like". So it makes sense to disallow access from outside the class. But if I'm accessing it from another object of the same class I and make any implementation changes I'm well aware of the changes and the accesses of the private members and can act accordingly.

Another thing to think about:

If class B extends class A, then any B-object also is an A-object, but it can't access the private A-methods. Why would that be if private methods were private to the object?

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