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

Discussion

Consider what happens when a subclass adds a field and compares it in equals. Which statement is correct?

  • A.Transitivity breaks but symmetry is preserved by the inherited implementation.
  • B.Symmetry can break.
  • C.Nothing breaks.
  • D.The compiler rejects the override.

Answer: B

A superclass instance compared with a subclass instance can return true while the reverse returns false, because each object applies its own equals. That asymmetry is why equals is hard to extend, and why favouring composition, or a getClass check instead of instanceof, is often recommended.

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

Post your comment

Please log in or register to post a comment.