Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!217.73.144.45.MISMATCH!feeder2.ecngs.de!ecngs!feeder.ecngs.de!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 18 Dec 2012 04:39:29 -0600 Date: Tue, 18 Dec 2012 10:39:27 +0000 From: lipska the kat User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120410 Thunderbird/11.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Need clarification on Object.equals. References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Lines: 56 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-ya9Tej2rK0hWYXSuAKKXIWKGnw1b/fOGdhi6vAI1BxBvSrOtorlJr6Kc2b3FWWhvfxCLxwwUxprOGso!xpZuBFtepluvdIDyJPt5bju80jo+vNV+P66F9qLJn+chSaPDuiopawr+iAg1dsqbYI5p5GZnVLE= X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2791 Xref: csiph.com comp.lang.java.programmer:20436 On 18/12/12 08:13, plewto@gmail.com wrote: > In the following code Node is an abstract class and both Gate and > Monitor are extensions of Node. a and b are distinct objects yet > a.equals(b) is returning true. > > public class Foo { > public static void main(String[] argv){ > Node a = new Gate(); > Monitor b = new Monitor(); > System.out.println(a.equals(b)); // --> prints 'true' > } > } According to the documentation the contract for equals on instances of class Object is as follows "The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)." This is obviously the not the case in your example above. Does the class Node or any of it's superclasses override the equals method ? The following code returns false as expected public abstract class Foo { public static void main(String args[]){ Foo bar = new Bar(); Baz baz = new Baz(); System.out.println(bar.equals(baz)); } } class Bar extends Foo{} class Baz extends Foo{} ... However, add the following method to the class Foo public boolean equals(Object obj){ return true; } and re-run the code and we get true. Has someone been messing with equals ? lipska -- Lipska the KatŠ: Troll hunter, sandbox destroyer and farscape dreamer of Aeryn Sun