Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Solution for my concrete example of mixed-type equals() with default value constraints instead of ignoring the subclass value fields. Instead of Angelika Langer's solution (see <a href="http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals-2.html" rel="nofollow">http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals-2.html</a>) with private methods _compareFields() and a protected method _navigateClassHierarchy() that has to be copied into each subclass, only a protected method compareOwnFields() is used, which has to be overridden correctly in each subclass. </p> <pre><code>class SuperClass { // ... @Override public final boolean equals(final Object other) { if (other == this) { return true; } if (!(other instanceof SuperClass)) { return false; } final SuperClass otherSuperClass = (SuperClass) other; return compareOwnFields(otherSuperClass, false) &amp;&amp; otherSuperClass.compareOwnFields(this, true); } protected boolean compareOwnFields(final SuperClass other, final boolean firstTraversal) { if (!firstTraversal) { return true; } if (field1 != other.getField1()) { return false; } // compare other fields similarly ... return true; } } class SubClass { // ... @Override protected boolean compareOwnFields(final SuperClass other, final boolean firstTraversal) { if (other instanceof SubClass &amp;&amp; !firstTraversal) { return true; if (other instanceof SubClass) { if (field1 != ((SubClass) other).getField1()) { return false; } // compare other fields similarly ... return super.compareOwnFields(other, firstTraversal); } else { if (field1 != DEFAULT_FIELD1) { return false; } // check other fields for default values similarly .. return super.compareOwnFields(other, firstTraversal); } } } </code></pre> <p>But this does not answer my question in general, it's rather a redesign that avoids the problem. So further answers on how to solve the problem with the Java language features are very welcome!</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.
 

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