首页 > 解决方案 > 为什么子类无法访问超类受保护变量?

问题描述

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

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.
    }
}

标签: javainheritanceprotected

解决方案


推荐阅读