title: Generics and the visitor pattern
body: <p>I am having a problem with the Visitor pattern and generics. I have some abstract class whose children are to be visited. Look at this code:</p>
  
  <pre><code>public abstract class Element extends SomeSuperClass {
      public void accept(Visitor&lt;? extends Element&gt; v) {
          v.visit(this);
      }
  }
  
  public interface Visitor&lt;T extends SomeSuperClass&gt; {
      void visit(T element);
  }
  </code></pre>
  
  <p>So the idea is: I have some class hierarchy (e.g. <code>Element</code> is a subclass of <code>SomeSuperClass</code>).
  I have got some generic <code>Visitor</code> interface to visit this hierarchy. Now in the middle of this hierarchy is the <code>Element</code> class, which is abstract and has it's own subclasses. </p>
  
  <p>Now I want <code>Element</code> to accept all visitors of its subclasses, which is why I put this line:</p>
  
  <pre><code>public void accept(Visitor&lt;? extends Element&gt; v)
  </code></pre>
  
  <p>But now I am receiving error:</p>
  
  <blockquote>
    <p>The method visit (<code>capture#1-of ? extends Element</code>) in the type <code>Visitor&lt;capture#1-of ? extends Element&gt;</code> is not applicable for the arguments (<code>Element</code>).</p>
  </blockquote>
  
  <p>I understand that <code>? extends Element</code> is not <code>Element</code>. My question is: Can I express my idea in a different way? Or I have just missed the idea of generics in this case?</p>