title: Alternative for multiple inheritance
body: <p>After 20 or so years programming Java for the first time I wish I had multiple inheritance. But I don't so I'm looking for an alternative for this concrete problem.</p>
  
  <p>The real application is some kind of ERP and somewhat complex so I try to translate this problem to cars. This only goes so far but it's the best I can do.</p>
  
  <p>Lets start with an interface describing what a car can do:</p>
  
  <pre><code>public interface Car {
  
      public String accelerate();
      public String decelerate();
  
      public String steerLeft();
      public String steerRight();
  }
  </code></pre>
  
  <p>Now we have a basic (but not abstract) implementation:</p>
  
  <pre><code>public class BasicCar implements Car {
  
      protected final String name;
  
      public BasicCar( String name ) {
  
          this.name = name;
      }
  
      // In the real word this method is important and does lots of stuff like calculations and caching
      protected String doDrive( String how ) {
          return name + " is " + how + "ing";
      }
      @Override
      public String accelerate() {
          return doDrive( "accelerat" );
      }
      @Override
      public String decelerate() {
          return doDrive( "decelerat" );
      }
  
  
      // This method is important, too
      protected String doSteer( String where ) {
          return name + " is steering to the " + where;
      }
      @Override
      public String steerLeft() {
          return doSteer( "left" );
      }
      @Override
      public String steerRight() {
          return doSteer( "right" );
      }
  }
  </code></pre>
  
  <p>In the real world this itself is a facade to a DAO. Note that I need the methods <code>doDrive</code> and <code>doSteer</code> to be there because this is where the real work like some calculations, translations and caching is done.</p>
  
  <p>And some more concrete implementations:</p>
  
  <pre><code>public class Sportscar extends BasicCar {
  
      public Sportscar( String name ) {
          super( name );
      }
  
      // I need to call the super method
      @Override
      public String doDrive( String how ) {
          return super.doDrive( how ) + " fastly";
      }
  }
  
  public class Truck extends BasicCar {
  
      public Truck( String name ) {
          super( name, new BasicMotor(), new TruckySteerer() );
      }
  
      // I need to call the super method
      @Override
      public String doSteer( String where ) {
          return super.doSteer( where ) + " carefully";
      }
  }
  </code></pre>
  
  <p>What I can do right now is:</p>
  
  <pre><code>Car mcqueen = new Sportscar( "McQueen" );
  mcqueen.steerLeft() //-&gt; "McQueen is steering left"
  mcqueen.accelerate() // -&gt; "McQueen is accelerating fastly"
  
  Car mack = new Truck( "Mack" );
  mack.steerLeft() //-&gt; "Mack is steering left carefully"
  mack.accelerate() // -&gt; "Mack is accelerating"
  </code></pre>
  
  <p><em>What I now want to do</em> is combining these two into one which shares their functionallity:</p>
  
  <pre><code>Car red = new Firetruck( "Red" );
  red.steerLeft() //-&gt; "Red is steering left *carefully*"
  red.accelerate() // -&gt; "Red is accelerating *fastly*"
  </code></pre>
  
  <p><em>What I tried / thought about</em></p>
  
  <p>I could copy&amp;paste code from both into one class. But this is never a good idea. And in the real application this is pretty much code so it's an even worse idea.</p>
  
  <p>I think I'm facing some major rewrite/refactoring here. </p>
  
  <p>So I could make <code>Sportscar</code> and <code>Truck</code> a <em>Decorator</em> and for <code>Firetruck</code> use both. But this won't work because <code>doSteer</code> and <code>doDrive</code> are called from within the decorated objects whereas a decorator only works for calles from the "outside". So I would have to put these methods in the decorator, too and this isn't a good idea either.</p>
  
  <p>So I could use Java8's new fancy features and make <code>doSteer</code> and <code>doDrive</code> delegating to an interface like:</p>
  
  <pre><code>@FunctionalInterface
  interface Driver {
      public String doDrive( String where );
  }
  </code></pre>
  
  <p>And feeding it to the constructor of <code>BasicCar</code>
  but then (apart from getting pretty complex and getting some other rather nasty java problems with <code>final</code>) I don't have access to the state of <code>BasicCar</code> in a good way anymore.</p>
  
  <p>So currently I'm a little lost here. Any good ideas would be appretiated.</p>
  
  <p>EDIT: To give an example how this looks in the real world: The <code>doSteer</code> group could be something like:</p>
  
  <pre><code>class ProductImpl {
      String getTitle(){
          return getField( "title" );
      }
      String getTeaser(){
          return getField( "teaser" );
      }
      String getField( String name ){
          Locale loc = configuredLocale();
          Map vars = configuredVarialbes();
          String value = getCached( name, loc );
          value = translateVariables( value );
          value = replaceVariables( value, vars );
          // and more
      }
  }
  </code></pre>