Question 1
What is the output of the following program?
public class Main {
public static void main(String[] args) {
Base b = new Kid();
System.out.println(b.v + " " + b.v());
}
}
class Base {
int v = 1;
int v() {
return v;
}
}
class Kid extends Base {
int v = 2;
int v() {
return v;
}
}A. 1 2Correct answer
Fields don't override, so b.v resolves by the reference type Base (1), while methods do, so b.v() runs Kid's override and reads Kid's own field (2) (JLS 8 §8.3, §15.12.4.4).
B. 2 2
This assumes fields override like methods; field access resolves by the reference type, giving Base's 1.
C. 1 1
The field half is right, but methods do override, so the call reaches the subclass version and its own field.
D. 2 1
Exactly backwards: field access is resolved by the reference type, while method dispatch uses the runtime type.
Explanation
Fields don't override — b.v resolves by the REFERENCE type Base: 1. Methods do — b.v() dispatches to Kid's override, which reads Kid's own v field: 2. Same name, opposite resolution rules, side by side.