title: Abstract class extensions return type
body: <p>I have an abstract class that is extended by two other classes. In the abstract class I have a method that is implemented differently by those two classes. I want to be able to use that method even if I don't know which concrete class object it is. Is there a way to make the return type the same as what the method was used on not the abstract type?</p>
  
  <p>To make it more concrete, the relevant part of abstract class:</p>
  
  <pre><code>public abstract class State&lt;abstarct&gt; {
  
  
  public abstract Set&lt;State&lt;?&gt;&gt; findNeighbors();
  </code></pre>
  
  <p>And for the other two classes:</p>
  
  <pre><code>public class PuzzleState extends State&lt;PuzzleState&gt; {
  
      @Override
  public Set&lt;PuzzleState&gt; findNeighbors() {
      // making the set
      // unrelevant to the question
      return set;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class QueensState extends State&lt;QueensState&gt; {
  
  @Override
  public Set&lt;QueensState&gt; findNeighbors() {
      // making the set
      // unrelevant to the question
      return set;
  }
  </code></pre>
  
  <p>For now it doesn't work of course. I just thought State might be what I was looking for but it isn't. Is there a way to make it work? I want the findNeighbors() to return either Set or Set according to what the method was used on. I want it to be that way because in another part of the program I have a display() method that takes as an input either PuzzleState or QueensState and displays it accordingly. </p>
  
  <p>I don't know if my question was clear enough, but basically I just want to know if there is a way to change the abstract methods return type according to the class.</p>