title: Distinguish between subclasses via equals & hashcode
body: <p>I am looking for guidance with regard to overriding both hashcode and equals in a subclass.</p>
  
  <p>I have found a similar question here: <a href="https://stackoverflow.com/questions/2066917/overriding-equals-hashcode-in-sub-classes-considering-super-fields">Overriding equals() &amp; hashCode() in sub classes ... considering super fields</a></p>
  
  <p>But what I want is something slightly different.</p>
  
  <p>Imagine this (somewhat stupid) example:</p>
  
  <pre><code>class Food {
  
    String name;
  
    @Override
    public boolean equals(Object obj) {
      if (obj instanceof Food) {
        Food other = (Food)obj;
        return name.equals(other.name);
      }
      return false;
    }
  
    @Override
    public int hashCode() {
      return name.hashCode();
    }
  }
  
  class Vegetable extends Food {
    // No additional fields here
    // Some methods here
  }
  
  class Fruit extends Food {
    // No additional fields here
    // Some methods here
  }
  </code></pre>
  
  <p>Given that:</p>
  
  <ol>
  <li>The subclasses do not add any extra fields
  
  <ul>
  <li>in this example at least, they are basically just marker classes</li>
  </ul></li>
  <li>A <code>Fruit</code> and <code>Vegetable</code> with the same name, should not be equal</li>
  </ol>
  
  <p>Questions:</p>
  
  <ol>
  <li>Would you expect the subclass equals to simply contain an <code>instanceof</code> check for the subclass and a call to <code>super.equals</code>?</li>
  <li>How should the hashcode be structured in an attempt to have <code>Fruit</code> and <code>Vegetable</code> instances with the same name have different hashcodes?</li>
  </ol>