Overriding Methods

AP Computer Science A· difficulty 3/5

public class Point {
    private int x, y;
    public Point(int a, int b) { x = a; y = b; }
    // no equals override
}
// Usage:
Point a = new Point(1, 2);
Point b = new Point(1, 2);
System.out.print(a.equals(b));

What is printed?

  • A

    0

  • B

    true

  • C

    Compile error

  • D

    false

    check_circle

Explanation

Without overriding equals, the inherited Object.equals compares references. a and b are different objects, so equals returns false.

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

Related questions