Skip to content

Latest commit

 

History

History
20 lines (15 loc) · 1.16 KB

aa68.md

File metadata and controls

20 lines (15 loc) · 1.16 KB

Back to questions

aa68: Symmetric equality testing

Recall from the end of question 5235 that you have implemented equals in a manner that is asymmetric in the presence of subclasses: you constructed a Point p and a ColouredPoint cp such that p.equals(cp) holds, but cp.equals(p) does not.

Think for a while about how to make equals work symmetrically in the presence of subclasses. If you manage to find an elegant working solution you should feel very happy, as this confounded Object Oriented Programming practitioners for some time! If you get stuck, then look at this excellent article on writing equality methods in Java:

The tutorial describes the "can equal" approach to defining equals in the presence of subclasses. This is the approach taken by the Scala programming language. There is a Java school of thought that believes that one should not override equals for classes that may have subclasses. According to this school of thought, what are the only kinds of classes for which equals should be overridden?