Thanks,
Ruben
All code in my posts, unless a source is explicitly mentioned, is my own.
All code in my posts, unless a source is explicitly mentioned, is my own.
I don't know why you are asking this. I don't see any reason to two objects of different classes be equals.
K & B 5.0, pg 527
First, be sure that the object being tested is of the correct type! It comes in polymorphically as type Object, so you need to do an instanceof test on it. Having two objects of different class types be considered equal is usually not a good idea, but that's a design issue we won't go into here. Besides, you'd have to do the instanceof test just to be sure that you could cast the object argument to the correct type so that you can access it's methods or variables in order to actually do the comparison. Remember, if the object doesn't pass the instanceof test, then you'll get a runtime ClassCastException.
Of course, I know that you can code around just about anything... but this demonstrates to me that there is a reason for adhering to that policy.
------------------------
Bob
SCJP - 86% - June 11, 2009
Bob Ruth wrote:I just took the following at face value:
K & B 5.0, pg 527
First, be sure that the object being tested is of the correct type! It comes in polymorphically as type Object, so you need to do an instanceof test on it. Having two objects of different class types be considered equal is usually not a good idea, but that's a design issue we won't go into here. Besides, you'd have to do the instanceof test just to be sure that you could cast the object argument to the correct type so that you can access it's methods or variables in order to actually do the comparison. Remember, if the object doesn't pass the instanceof test, then you'll get a runtime ClassCastException.
Of course, I know that you can code around just about anything... but this demonstrates to me that there is a reason for adhering to that policy.
I was aware of that also, Bob. I was wondering why there is no official contract specification in the API regarding this, as it indeed seems it would be a really bad idea to consider two objects of unrelated classes be equal to each other.
All code in my posts, unless a source is explicitly mentioned, is my own.
Having two objects of different class types be considered equal is usually not a good idea, but that's a design issue
I think this pretty much answers the question. Yes it is a bad idea to do it but there are some scenarios where it could be done, e.g. implementing a balanced binary red/black tree, again not the best approach but certainly possible.
The reason I quoted the contract earlier is that if we code the equals method in a.class to allow for equality to b.class we will have to do the same to b.class equal method. This means that the designer intentionally designed the two classes in that manner. As far as API is concerned, if it walks like a duck and talks like a duckā¦
Henry Wong wrote:Try this...
And of course, an ArrayList is NOT an instanceof LinkedList.
Henry
Excellent example, thanks! I hadn't thought of it, but it makes the point very clearly. The List interface specifies expected behavior for equals(), which in this case is that any two instances of implementing classes of List of the same length, and whose corresponding elements are equal, are considered equal.
All code in my posts, unless a source is explicitly mentioned, is my own.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |