title: Strategy pattern with generics in Java
body: <p>So I'm trying to create an interface with a generic function to be overrided by a subclass but I can't find a way to do this.</p>
  
  <p>I think a piece of code will be more explicit:</p>
  
  <pre><code>interface Agent&lt;A extends Agent&lt;A&gt;&gt; {
      &lt;W extends World&gt; Iterable&lt;Action&lt;A,W&gt;&gt; percepts(W world);
      &lt;W extends World&gt; void takeAction(Action&lt;A,W&gt; action, W world);
  }
  
  abstract class StrategistAgent&lt;A extends StrategistAgent&lt;A&gt;&gt; implements Agent&lt;A&gt; {
      abstract public &lt;W extends World&gt; AgentStrategy&lt;A,W&gt; getStrategy();
      abstract public &lt;W extends World&gt; void setStrategy(AgentStrategy&lt;A,W&gt; strategy);
  
      @Override
      &lt;W extends World&gt; Iterable&lt;Action&lt;A,W&gt;&gt; percepts(W world) {
          return getStrategy().run(this, world);
      }
  }
  
  interface AgentStrategy&lt;A extends StrategistAgent&lt;A&gt;, W extends World&gt; {
      Iterable&lt;Action&lt;A,W&gt;&gt; run(A agent, W world);
      void init(A agent, W world);
  }
  </code></pre>
  
  <p>error: The method run(A, World) in the type AgentStrategy is not applicable for the arguments (StrategistAgent, W)</p>
  
  <p>but I cannot write :</p>
  
  <pre><code>StrategistAgent&lt;A extends StrategistAgent&lt;A&gt;, W extends World&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong> Ok I followed Mark Peters' advice and removed the setStrategy method. Should be working now..</p>