简体   繁体   中英

protected can't access in different package subclass why?

Error: m1() has protected access in A

When I try to using superclass reference variable ( A obj=new B() )

This is a first class in package pkg1;

package pkg1;
public class A {
    protected void m1() {
        System.out.println("protected method");
    }
}

This is a second class which is in another package pkg2 importing pkg1;

package pkg2;

import pkg1.A;
public class B extends A {
    @Override
    protected void m1() {
        System.out.println("override m1");
    }

    public static void main(String ar[]) {
        A obj=new B();
        obj.m1();
    }
}

I feel like you're still slightly confused with why protected m1() isn't visible.

You understand that main is a method in B and B is a subclass of A , therefore you feel like it should be accessible.

The key is obj is cast to be interpreted of type A . Then you try to call instance method m1 from static context in B (see this link above for why this is important). By class A definition, m1 is accessible through:

  1. Class definition itself on a given instance of itself.
//example 
package pkg1;

public class C {
    protected void m1() {
        System.out.println("protected method");
    }

   public static void callM1(C anInstanceOfC){
        anInstanceOfC.m1();
   }
}
package pkg2;

import pkg1.C;
public class D extends C {
    @Override
    public void m1() {
        System.out.println("override m1");
    }

    public static void main(String ar[]) {
        C obj=new D();
        C.callM1(obj);//output would be "override m1"
    }
}
  1. Instance methods (of course) of the class within itself and of subclasses.
//example 
package pkg1;

public class C {
    protected void m1() {
        System.out.println("protected method");
    }

    public void m2() {
        m1();//call to instance method m1
    }

   public static void callM1(C anInstanceOfC){
        anInstanceOfC.m2();
   }
}
package pkg2;

import pkg1.C;
public class D extends C {
    @Override
    public void m1() {
        System.out.println("override m1");
    }

    public void m3() {
        //super.m2()
        m2();//call to superclass method
    }

    public static void main(String ar[]) {
        D obj=new D();
        D.m3(); // output "override m1"
    }
}

protected keyword means that you can access protected-defined data from child classes. In example, you try to access protected data from non-child static context. You should probably try this:

package pkg2;

import pkg1.A;
public class B extends A {
    @Override
    public void m1() {
        System.out.println("override m1");
    }

    public static void main(String ar[]) {
        B obj=new B();
        obj.m1();
    }
}

The access level of m1() is protected. Protected methods can only be accessed by subclasses or by other classes in the same package . From your static 'main' method, you can not call a protected instance method of a class in a different package. If class B was in the same package as class A, then you will have no error and your code would work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM