title: Define abstract methods with generics
body: <p>I've been reading a lot on generics but I still have some basic problems implementing a simple hierarchy in Java. The goal is to define an abstract method that will compare two objects of the same class, if the objects are from different classes it should return false. Example:</p>
  
  <p>We define the abstract class Sound</p>
  
  <pre><code>abstract class Sound{
      public boolean check(Sound d){return false;};
  }
  </code></pre>
  
  <p>Some classes that extend Sound and another abstract class that extends from it, like Alarm</p>
  
  <pre><code>abstract class Alarm extends Sound{
      @Override
      public boolean check(Alarm d){//Do something and return boolean};
  }
  </code></pre>
  
  <p>With some more classes that extend from it.</p>
  
  <p>Simple solution would be to define the method as its shown and it will work, BUT I feel there's a better way to enforce the hierarchy so the Sound class defines that the method should be used only with a parameter that is of the same class.</p>
  
  <p>What I've tried with Generics:</p>
  
  <pre><code>abstract class Sound{
      public &lt;T extends Sound&gt; boolean check(T d){return false;};
  }
  
  abstract class Alarm extends Sound{
      @Override
      public &lt;T extends Alarm&gt; boolean check(T d){//Do something and return boolean};
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>abstract class Alarm extends Sound{
      @Override
      public boolean check(Alarm d){//check and return boolean};
  }
  </code></pre>
  
  <p>Java complains because to the compiler I'm not overriding the check method in Alarm.
  Any clue on what could be missing?</p>