Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You must be careful when overriding equals to adhere to a specific set of rules. See the <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#equals%28java.lang.Object%29" rel="noreferrer">javadoc</a> for details. In short, two tricky parts are preserving symmetry and transitivity. According to Joshua Block's <a href="http://rads.stackoverflow.com/amzn/click/0321356683" rel="noreferrer">Effective Java</a> </p> <blockquote> <p>"There is no way to extend an instantiable class and add a value component while preserving the equals contract"</p> </blockquote> <p>What does this mean? Well let's say you have in class A a property of type T and in subclass B another property of type V. If both classes override equals then you'll get different results when comparing A to B than B to A.</p> <pre><code>A a = new A(T obj1); B b = new B(T obj1, V obj2); a.equals(b) //will return true, because objects a and b have the same T reference. b.equals(a) //will return false because a is not an instanceof B </code></pre> <p>This is a violation of symmetry. If you try and correct this by doing mixed comparisions, you'll lose transitivity.</p> <pre><code>B b2 = new B(T obj1, V obj3); b.equals(a) // will return true now, because we altered equals to do mixed comparisions b2.equals(a) // will return true for the same reason b.equals(b2) // will return false, because obj2 != obj3 </code></pre> <p>In this case b == a, b2 ==a, b != b2, which is a problem.</p> <p><em>EDIT</em> </p> <p>In an effort to more precisely answer the question: "what will make the resulting code not working correctly with the generated methods" let's consider this specific case. The parent class is abstract and does not override equals. I believe we can conclude that the code is safe and no violation of the equals contract has occurred. This is a result of the parent class being abstract. It cannot be instantiated, therefore the above examples do not apply to it.</p> <p>Now consider the case when the parent class is concrete and does not override equals. As Duncan Jones pointed out, the warning message is still generated, and in this case seems correct to do so. By default, <em>all</em> classes inherit equals from Object, and will be compared based on object identify (i.e. memory address). This could result in a unsymmetrical comparison if used with a subclass that does override equals. </p> <pre><code>A a = new A(); B b = new B(T obj1); a.equals(b) //will return false, because the references do not point at the same object b.equals(a) //should return false, but could return true based on implementation logic. </code></pre> <p>If b.equals(a) returns true for whatever reason, either implementation logic or programming error, a loss of symmetry will result. The compiler has no way to enforce this, hence, a warning is generated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload