title: Java generics - overriding an abstract method and having return type of the subclass
body: <p>I am trying to create a set up where a set of subclasses override a superclass. This superclass contains an abstract method - the return type of which would ideally be that of the object from which this method was called, such that it effectively behaves like this:</p>
  
  <pre><code>public abstract class SuperClass{
    public abstract SuperClass getSelf();
  }
  
  public class SubClass extends SuperClass{
    @Override
    public SubClass getSelf(){
      return this;
    }
  }
  </code></pre>
  
  <p>I am unsure if such a thing is possible, as I think return types always have to be the same in order for the override to work - however I have been thinking the answer, should one exist, lies somewhere along this line...</p>
  
  <pre><code>public abstract class SuperClass{
    public abstract &lt;? extends SuperClass&gt; getSelf();
  }
  
  public class SubClass extends SuperClass{
    @Override
    public SubClass getSelf(){
      return this;
    }
  }
  </code></pre>
  
  <p>Thanks for any help.</p>
  
  <p>edit: added extends SuperClass to SubClass, duh</p>