Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of these statements about what the equals method must accept as its parameter to override Object.equals is correct?

  • A.It must take a parameter of the class's own type.
  • B.It may take either an Object or the class's own type; both count as overriding.
  • C.It must take a single Object parameter and be declared final.
  • D.It must take a single Object parameter; a narrower parameter type only overloads it.

Answer: D

Overriding requires the same signature, so public boolean equals(Point p) is a new overload and Object.equals is left in place. Collections call the inherited version through an Object reference, so the intended logic is never reached. Adding @Override turns the mistake into a compile error.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.