It is probably a bad idea for the reasons already stated in other answers.
As for the "legal" aspects, the Contract of Object.equals states
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value x, x.equals(x) should return true.
- It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
- It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
- It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
- For any non-null reference value x, x.equals(null) should return false.
Step by step:
- reflexive: Yes, due to
this == o
- symmetric: due to the use of
instanceof
, we would need to look at all super- and subclasses to be sure
- transitive: depends on the symmetry-requirement, otherwise Yes
- consistent: Yes
x.equals(null)
should return false: Yes, due to instanceof
So from a purely legal point of view, it depends on whether other implementations accross your inheritance-hierarchy violate the symmetry and transitivity - see the answers to Any reason to prefer getClass() over instanceof when generating .equals()?.
But aside from that, given the fact that hashCode
is not required to produce different values for non-equivalent instances, it is usually not a good way to define equality.
An example:
A immutable point class with two fields x
and y
class Point {
final int x;
final int y
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
-> there are 2^32 * 2^32 = 2^64
distinct states, but only 2^32
possible hashcodes. This means that there are lots of points that would be considered equal according to your implementation of equals
.
Also see this example equals and hashCode: Is Objects.hash method broken? for someone stumbling over a hash-collision for hashes created with Objects.hash
with Strings
.