Quick revision
The short answer
When ColorPoint adds a color to Point, any equals we write breaks a rule: comparing color only on the ColorPoint side breaks symmetry, ignoring color when the other object is a plain Point breaks transitivity, and comparing getClass() keeps both rules but breaks Liskov substitution for harmless subclasses. As Effective Java puts it, there is no way to extend an instantiable class with a value component and keep the equals contract. The practical options are composition, where ColorPoint has a Point, or final value classes and records, which cannot be extended at all.
What to remember
In this answer, we tried three versions of ColorPoint.equals and saw that each breaks something: checking color only on one side breaks symmetry, ignoring color for plain Points breaks transitivity, and comparing getClass() breaks substitution for subclasses that add no state. Effective Java's conclusion is that an instantiable class cannot be extended with a value component without breaking the contract, so we prefer composition and make value classes final or records.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
When ColorPoint adds a color to Point, any equals we write breaks a rule: comparing color only on the ColorPoint side breaks symmetry, ignoring color when the other object is a plain Point breaks transitivity, and comparing getClass() keeps both rules but breaks Liskov substitution for harmless subclasses. As Effective Java puts it, there is no way to extend an instantiable class with a value component and keep the equals contract. The practical options are composition, where ColorPoint has a Point, or final value classes and records, which cannot be extended at all.
Why this question is asked
Inheritance and equality do not mix well, and ColorPoint is the textbook example of why. The question matters in real code because domain models often grow subclasses that add one field, and the resulting equals bugs show up as sets that keep or lose elements depending on the order of operations. Interviewers ask it at this level to see whether we can reason about the contract rather than recite it. In this answer, we will recap the two rules at stake, then try three versions of ColorPoint.equals and see exactly which rule each one breaks, and finish with the designs that avoid the problem.
1A Quick Recap of Symmetry and Transitivity
Symmetry means that if a.equals(b) is true, then b.equals(a) must also be true. Transitivity means that if a equals b and b equals c, then a must equal c. Hash sets and maps depend on both. A set may call equals on the element it holds or on the one we pass in, and it may compare elements in any order, so if two objects disagree about whether they are equal, the result depends on details we do not control.
Here is the parent class. Point compares x and y and accepts any Point, including subclasses, through instanceof.
2Attempt 1: ColorPoint Also Checks Color, and Symmetry Breaks
The obvious first version makes ColorPoint compare color as well, and only accept other ColorPoints.
Notice that each class is internally reasonable. The problem is that they disagree. If we put p in a HashSet and ask whether it contains cp, the answer depends on which object the set calls equals on, and that is an implementation detail.
3Attempt 2: Ignore Color for Plain Points, and Transitivity Breaks
To fix symmetry, we can make ColorPoint behave like Point when the other object is a plain Point, and compare color only between two ColorPoints.
Now symmetry holds for each pair, but the chain red equals p and p equals blue does not lead to red equals blue. A set holding red and blue would also hold neither or both of them depending on whether p was added first. This version has a second danger: if two different subclasses both use the trick, each delegates to the other and the calls can recurse until StackOverflowError.
4Attempt 3: Compare getClass(), and Substitution Breaks
A common suggestion is to make Point compare exact classes instead of using instanceof. Then a Point is never equal to any subclass instance, and both rules hold.
| Attempt | Symmetric? | Transitive? | Subclass usable as Point? |
|---|---|---|---|
Check color, accept only ColorPoint | No | Yes | Yes |
| Ignore color against plain Point | Yes | No | Yes |
Compare getClass() in Point | Yes | Yes | No: breaks Liskov substitution |
This is the cost. The Liskov substitution principle says a subclass instance should be usable wherever the parent is expected. CountingPoint adds no state at all, yet it can never be found in a set of Points. So getClass() trades a contract violation for a design violation: equality now depends on the exact class rather than on what the object represents. It is a legitimate choice in some frameworks, but it is not a solution to the original problem.
5Why No Version Can Work
Effective Java states the conclusion directly: there is no way to extend an instantiable class and add a value component while preserving the equals contract, unless we give up the benefits of object-oriented abstraction. The reason is that Point already promised that equality means same x and same y. A subclass that wants equality to also mean same color is redefining what equality means for objects that are also Points, and the parent's promise and the child's promise cannot both be kept.
Notice the word instantiable. If Point were abstract, there would be no plain Point instances to compare against, and each concrete subclass could define equality over its own fields without conflict. The problem only appears when the parent itself has instances with their own, narrower idea of equality.
6The Way Out: Composition, Final Classes and Records
The recommended fix is composition: ColorPoint does not extend Point; it has a Point. It can still offer a view of itself as a Point when callers need one.
Now a ColorPoint and a Point are simply different types, and each comparison is honest. Records make this the natural design: they are final, so no subclass can add a component, and their generated equals compares all components. The general rule for value classes is the same: make them final, or records, and use composition when a variant needs more data.
7Follow-up questions
Is getClass() comparison ever the right choice?
It is reasonable when subclasses are expected to add state and we never want cross-class equality, and some frameworks generate equals this way. The price is that subclasses without new state, including proxies created by libraries, are never equal to the parent, so with JPA proxies, for example, getClass() comparisons need extra care.
Does the same problem affect compareTo?
Yes. A subclass that adds a field cannot extend the parent's compareTo ordering and stay consistent with the parent's rules either. The same answer applies: favour composition, and give each type its own Comparator when different orderings are needed.
What if the subclass adds a field that is not part of equality?
Then there is no problem. A subclass can add a cache, a counter or a logger and keep using the parent's equals unchanged, because it does not change what equality means. The difficulty is specific to adding a value component, a field that should affect equality.
8Conclusion
In this answer, we tried three versions of ColorPoint.equals and saw that each breaks something: checking color only on one side breaks symmetry, ignoring color for plain Points breaks transitivity, and comparing getClass() breaks substitution for subclasses that add no state. Effective Java's conclusion is that an instantiable class cannot be extended with a value component without breaking the contract, so we prefer composition and make value classes final or records.
Open with
The problem is that Point has already defined equality as same x and same y, and ColorPoint wants to change that definition for objects that are still Points.
Then, if they want more
If ColorPoint checks color and only accepts other ColorPoints, then a point equals a color point but not the other way round, so symmetry is broken.
If I fix that by ignoring color when the other object is a plain Point, then red equals the plain point, the plain point equals blue, but red does not equal blue, so transitivity is broken, and two subclasses doing this can even recurse forever.
If I switch Point to compare getClass(), both rules hold, but now a subclass that adds no state at all, like one that just counts instances, can never be found in a set of Points, which violates Liskov substitution. Effective Java's conclusion is that you cannot extend an instantiable class with a value component and keep the contract.
So I would use composition: ColorPoint has a Point and exposes asPoint(), and I would make value classes final, ideally records, so nobody can subclass them in the first place. If the parent were abstract, the problem would not arise.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why is it hard to keep equals symmetric and transitive when a subclass adds a value...": https://scrivio-interview-library.netlify.app/java/object-contracts/69c8baeaa0/