Overriding Methods

AP Computer Science A· difficulty 2/5

public class Point {
    private int x, y;
    public Point(int x, int y) { this.x = x; this.y = y; }
    public String toString() { return "(" + x + "," + y + ")"; }
}
System.out.println(new Point(3, 4));

What is printed?

  • A

    (3,4)

    check_circle
  • B

    Point@<hash>

  • C

    3,4

  • D

    Compile-time error

Explanation

<code>println(Object)</code> calls <code>toString()</code>. The override returns "(3,4)".

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

Related questions