在Java中,继承成员变量和继承方法的表现是不同的。

public class Test {

    public static void main(String[] args) {
        Base base = new Base();
        System.out.println(base.count);
        base.display();

        Derived derived=new Derived();
        System.out.println(derived.count);
        derived.display();

        Base bd = new Derived();
        System.out.println( bd.count);
        bd.display();

        bd = derived;
        System.out.println(bd.count);
        bd.display();

    }
}
class Base{
    int count=2;
    public void display(){
        System.out.println("base:"+count);
    }
}

class Derived extends Base{
    int count=20;
    public void display(){
        System.out.println("derived:"+count);
    }
}

输出的结果是:

2
base:2
20
derived:20
2
derived:20
2
derived:20

进行调试的话,可以看到,Derived的实例对象是长这样子的

count = 20;
Base.count =2 ;

即子类和父类的成员变量各有自己的内存空间。