Why subclass in another package cannot access a protected method?

后端 未结 7 2221
一向
一向 2020-11-30 05:07

I have two classes in two different packages:

package package1;

public class Class1 {
    public void tryMePublic() {
    }

    protected void tryMeProtect         


        
7条回答
  •  佛祖请我去吃肉
    2020-11-30 05:34

    the protected modifier is 1.Package Private 2.can be seen by subclasses from other packages. now the key difference between :

    MyClass1 c = new MyClass1();
        c.tryMeProtected();
    

    and

    tryMyProtected(); 
    

    is that the MyClass1 reference is used rather than inheritance. MyClass1 is in a different package and this code is not inheriting from MyClass1.

提交回复
热议问题