class A
{
int a = 2;
public void display()
{
System.out.println(a);
}
}
class B extends A
{
int b;
public B()
{
b = a+1;
}
public void display()
{
System.out.println(b);
}
}
class InheritanceDemo
{
public static void main(String [] args)
{
A a = new B();
// it is ok
a.display();
// let's try another way
// you will find, it is wrong
B b = new A();
// wrong code
,
将这两行屏蔽掉,然后再试
b.display();
// so this b does not exist
// and now we try a normal way
A anotherA = new A();
anotherA.display();
// and we try this normal way
B anotherB = new B();
anotherB.display();
}
}