This is a question from mock exam, and the output is value is 2 value is 3;
I can't understand why the b.x=3 instead of 2, anyone explain this for me? Thanks a lot!
class Base {
int x=3;
public Base() {}
public void show() {
System.out.print(" The value is " + x);
}
}
class Derived extends Base {
int x=2;
public Derived() {}
public void show() {
System.out.print(" The value is " + x);
}
}
public class
Test {
public static void main(
String args[]) {
Base b = new Derived();
b.show();
System.out.println("The value is " +b.x);
}
} // end of class Test