Why subclass in another package cannot access a protected method?

后端 未结 7 2228
一向
一向 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:28

    As per Java Protected Access modifier definition methods which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

    you can't access protected method by creating object of class. So for accessing Protected method you have to extend the superclass.(this explains your 2nd call is correct)

提交回复
热议问题