Object Superclass

AP Computer Science A· difficulty 3/5

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?

  • A

    false

    check_circle
  • 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.

Want 10 more like this — adaptive to your weak spots?

Related questions