title: Is it possible to extend a generic class while restricting the generic type?
body: <p>Suppose I have abstract classes (or, indeed, interfaces)</p>
  
  <pre><code>public abstract class Animal
  
  public abstract class Bird Extends Animal
  </code></pre>
  
  <p>and a generic class</p>
  
  <pre><code>public class Lifestyle&lt;A extends Animal&gt;
  </code></pre>
  
  <p>So we might create a <code>Lifestyle&lt;Lion&gt;</code> object.  </p>
  
  <p>The <code>Lifestyle</code> class contains methods that animals can use to tell them how to walk around, find food, interact with other animals of the same species, etc.  Now suppose I want to extend this class to a special <code>BirdLifestyle</code> class that tells <code>Bird</code>s how to do all the above things, but also tells them how to <code>fly</code> and use all of the extra methods in the <code>Bird</code> class.  I want to be able to create a <code>BirdLifestyle&lt;Eagle&gt;</code> object, for instance.  I'm pretty sure that the following won't compile: </p>
  
  <pre><code>public class BirdLifestyle&lt;B extends Bird&gt; extends Lifestyle&lt;A extends Animal&gt;
  </code></pre>
  
  <p>and the only alternative I can think of is rather nasty: </p>
  
  <pre><code>public class BirdLifestyle&lt;B extends Bird&gt;
  {
    private Lifestyle&lt;B&gt; lifestyle // tells the bird how to do animal things.
  
    public Lifestyle&lt;B&gt; getLifestyle()
    {
      return lifestyle;
    }
  
    // Bird-specific methods.
  }
  </code></pre>
  
  <p>We could then get all of the methods from <code>Animal</code> by calling <code>getLifestyle().walk()</code> or things like that.  </p>
  
  <p>Now suppose that my friends have all created their own implementations of these four classes and that we want to link them using interfaces.  So we create</p>
  
  <pre><code>public interface LifestyleInterface
  {
    public void walk();
  
    // etc.
  }
  
  public interface AvianLifestyleInterface extends LifestyleInterface
  {
    public void fly();
  
    // etc.
  }
  </code></pre>
  
  <p>My friends are all more specialize, so they've all written things like: </p>
  
  <pre><code>public class LionLifestyle implements LifestyleInterface
  </code></pre>
  
  <p>or</p>
  
  <pre><code>public class EagleLifestyle implements AvianLifestyleInterface
  </code></pre>
  
  <p>while I can write: </p>
  
  <pre><code>public class Lifestyle&lt;A extends Animal&gt; implements LifestyleInterface
  </code></pre>
  
  <p>But I <em>can't</em> now write: </p>
  
  <pre><code>public class BirdLifestyle&lt;B extends Bird&gt; implements AvianLifestyleInterface
  </code></pre>
  
  <p>even if my <code>BirdLifestyle</code> class overrides all the methods introduced in <code>AvianLifestyleInterface</code>.  This is because <code>BirdLifestyle</code> is not a superclass of <code>Lifestyle</code>.  The only way round this is to create lots of entry-point methods such as:</p>
  
  <pre><code>public class BirdLifestyle&lt;B extends Bird&gt;
  {
    private Lifestyle&lt;B&gt; lifestyle;
  
    public Lifestyle&lt;B&gt; getLifestyle()
    {
      return lifestyle;
    }
  
    // 'AvianLifestyleInterface' methods.
  
    @Override
    public void fly()
    {
      // Code for flying.
    }
  
    // etc.
  
    // 'LifestyleInterface' methods.
  
    @Override
    public void walk()
    {
      getLifestyle().walk();
    }
  
    // etc., creating a similar one-line method for each method in 
    // 'LifestyleInterface' that is just an entry-point to the same 
    // method in the 'Lifestyle&lt;A&gt;' object.
  }
  </code></pre>
  
  <p>This seems like an unnecessary amount of code to write, and a lot of the code is written in a fairly mechanical way, which breaks several programming rules.  For example, if I want to add any methods to the <code>LifestyleInterface</code> interface then I need to remember to add a new one-line method into the <code>BirdLifestyle</code> class.  Is there a cleaner way?  </p>