为什么子类无法访问超类保护变量?

时间:2018-09-08 05:04:47

标签: java inheritance protected

我试图理解为什么子类无法访问超类中的受保护变量?

package pack1;

public class Parent {

    protected int r;

    public int s;
}



package pack2;

import pack1.Parent;

public class Child extends Parent {

    public static void main(String[] args) {

        Parent obj = new Child();
        obj.r = 2;      //r is not accessible here. It becomes accessible when I make it static.
        obj.s = 3;      //s is accessible.
    }
}

0 个答案:

没有答案