public class W {
private int x;
public W(int x) { this.x = x; }
public boolean equals(W o) {
return o.x == this.x;
}
}
// ...
W a = new W(5);
Object o = new W(5);
System.out.println(a.equals(o));What is printed?
- Acheck_circle
false
- B
true
- C
5
- D
Compile-time error
Explanation
The custom equals(W) does not override Object.equals(Object); when called with an Object reference the inherited equals (reference comparison) is used, returning false.