Article Discussion
How to Write an Equality Method in Java
Summary: This article describes a technique for overriding the equals method that preserves the contract of equals even when subclassses of concrete classes add new fields.
48 posts on 4 pages.      
« Previous 1 2 3 4 Next »
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: August 6, 2019 11:01 AM by kirsty
Alex
Posts: 1 / Nickname: alexv83 / Registered: February 7, 2016 3:32 PM
Re: How to Write an Equality Method in Java
February 7, 2016 9:41 PM      
about point number 3.
I think it is a very bad idea, not overriding the equals() Method.

I think the best thing to do is to understand ones you have changed the object, you have! to remove! the old one from the hash, and enter the new one. yes it might be little less efficient, but it will save you from troubles, and it is logically correct. that is my opinion.
Vasya
Posts: 1 / Nickname: markyy86 / Registered: June 5, 2019 10:17 AM
Re: How to Write an Equality Method in Java
June 5, 2019 10:38 AM      
How Joshua Bloch writes there is no general solution when you try to create subclass adding fields that changes the state of entity.
The solution with canEqual looks like a workaround.
In OOP the problem could be solved by separating interface and implementation. Very strange that no one wrote this before, or did I miss something?

interface Point {
  int getX();
  int getY();
}
 
interface ColoredPoint extends Point {
  Color getColor();
}
 
// lombok annotations
@Getter
@AllArgsConstructor
// compare by own fields x, y
@EqualsAndHashCode(of = {"x", "y"})
class PointImpl implements Point {
    private final int x;
    privaet final int y;
}
 
@Getter
@AllArgsConstructor
// compare by own fields point, color
@EqualsAndHashCode(of = {"point", "color"})
class ColoredPointImpl implements ColoredPoint {
    private final Point point; // just delegate getX, getY to point
    private final Color color;
}
 



in that way the equality and Liskov principle could be satisfied.
kirsty
Posts: 1 / Nickname: iculantio / Registered: July 23, 2019 9:24 AM
Re: How to Write an Equality Method in Java
August 6, 2019 11:01 AM      
> The benefit of
> instanceof/canEqual over just using
> getClass is that subclasses instances can
> equal superclass instances. The writer of the subclass
> decides whether this is possible based on whether or not
> he or she overrides the canEqual method.

Is it not the case, however, that if the subclass overrides equals() and doesn't override canEqual() to return {that instance of this}, we are back in non-compliance territory.

The only benefit I see to this is that the subclass controls the behavior instead of the superclass. That's better but it doesn't address the fundamental issue of designing a part of a system using the equals() method as defined in my base class. If I depend on equals to determine if two points have the same x,y coordinates (e.g. use collections naively) canEquals() solves nothing.
48 posts on 4 pages.
« Previous 1 2 3 4 Next »