So the scenario is as follows:
class Feline
{
String name;
int age;
equals(Object obj) {...}
hashCode(){...}
}
class Cat extends Feline
{
int teeth;
hashCode(){...}
equals(Object obj)
{
if (!super.equals(obj))
{
return false; //If I don't want this should I use
}
...
}
}
The issue is in reality this inheritance is correct, but to the program it is not necessarily true. My thought on this is Cat
should actually be composed of a Feline
object. The question is, which of these approaches should I take?
EDIT
This is the implementation from Eclipse, default equals/hashcode. It may be that the implementation of equals is not the most accurate way to do this.
Ohoh equality checking in the face of inheritance. That's EXTREMELY hard to get right and rather long to describe.
The correct solution isn't as straight forward as one would think, so please go read this - that should clear up all your questions. If not feel free to ask again :)
Edit: So as a short summary of the above link: An equality method should fulfill the following properties:
To guarantee that this works, we need to specify another method: public boolean canEqual(Object other)
. This method should return true if the other object is an instance of the class in which canEqual is (re)defined, false otherwise.
In other words the method must always be overwritten if we overwrite equal()
itself. The implementation itself is trivial, a short example:
class Foo {
public boolean canEqual(Object other) {
return other instanceof Foo;
}
// equals implementation, etc.
}
The equals method itself must always first check if the given object canEqual itself, e.g. something along the lines of other.canEqual(this) && restOfComparison
.
Short example of a class extending the above Foo:
class Bar extends Foo {
public boolean equals(Object other) {
if (other instanceof Bar) {
Bar that = (Bar)other;
return that.canEqual(this) && otherStuff;
}
return false;
}
public boolean canEqual(Object other) {
return other instanceof Bar;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With