Okay so here is a tricky example from my lecture notes:
I would expect the program to print out 10, but it actually prints out 0... Can anyone explain that?:D
Thanks!
Code:
class Base {
private int val;
Base () {
val = lookup();
}
public int lookup() {
return 5;
}
public int value() {
return val;
}
}
class Derived extends Base {
private int num=10;
public int lookup() {
return num;
}
}
class Test {
public static void main( String[] args ) {
Derived d = new Derived();
System.out.println(d.value());
}
}
Code:
java -version java version "1.6.0_18" OpenJDK Runtime Environment (IcedTea6 1.8) (fedora-41.b18.fc13-x86_64) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)
Comment