need a explanation!

I came accross a scjp MCQ that is,

class A{

int x=10;

void method(){

System.out.println("A prints"+x);

}

}

class Bextends A{

int x=20;

void method(){

System.out.println("B prints "+x);

}

}

class c{

publicstaticvoid main(String args[]){

A y=new B();

y.method();//Line P

System.out.println(y.x);//Line Q

}

}

When i compile the code it will give the output as "B prints 20" and "10". But i am bit confuse how come the "Line P" access the "x" in class B and "Line Q" access the variable in class A instead of class B.Can anyone give me a explanation how this happened and what realy happens in this code.

[1551 byte] By [ram102125a] at [2007-11-27 7:42:00]
# 1
That's called a shadowed variable. Instead of B setting the variable that it inherits from A, it creates it's own separate variable. Calling B.method accesses B's x variable. A.x accesses the A class' x variable. You can google for java shadow variable for more info.
hunter9000a at 2007-7-12 19:22:44 > top of Java-index,Java Essentials,New To Java...