Re: Need clarification on Object.equals.
On 12/19/2012 8:13 AM, FredK wrote:
On Tuesday, December 18, 2012 10:56:19 AM UTC-8, Peter Duniho wrote:
On Tue, 18 Dec 2012 04:48:16 -0800, Roedy Green wrote: > On Tue, 18 Dec 2012 10:39:27 +0000, lipska the kat > <lipskathekat@yahoo.co.uk> wrote, quoted or indirectly quoted someone > who said : > >>> Node a = new Gate(); >>> Monitor b = new Monitor(); >>> System.out.println(a.equals(b)); // --> prints 'true' > > Gate or one of its superclasses is implementing equals. And doing it incorrectly, I'd say.
Why would you think it was done incorrectly?
For most classes, a.equals(b) is not the same as a==b.
Think about string:
string a = "abc");
string b = new string(a);
Clearly a==b is false, but a.equals(b) is true.
Usually the equals() method returns true if the internal state of both objects is the same.
I think "inappropriately" might have been a better word than
"incorrectly". The inherited .equals method was a fine .equals method
for a Set. Indeed, it does exactly what a Set .equals method is required
to do, according to the Set contract.
The problem was that AbstractSet was being extended by a class that is
apparently not intended to be used as a Set implementation. The
inherited .equals method was not an appropriate .equals method for Node.
Patricia