title: How to Maximize Code Reuse in this Interface vs Inheritance C# Example
body: <p>Inspired by <a href="https://www.youtube.com/watch?v=wfMtDGfHWpA">a great video</a> on the topic "Favor object composition over inheritance" which used JavaScript examples; I wanted to try it out in C# to test my understanding of the concept, but it didn't go as well as I'd hoped.</p>
  
  <pre><code>/// PREMISE
  // Animal base class, Animal can eat
  public class Animal
  {
      public void Eat() { }
  }
  // Dog inherits from Animal and can eat and bark
  public class Dog : Animal
  {
      public void Bark() { Console.WriteLine("Bark"); }
  }
  // Cat inherits from Animal and can eat and meow
  public class Cat : Animal
  {
      public void Meow() { Console.WriteLine("Meow"); }
  }
  // Robot base class, Robot can drive
  public class Robot
  {
      public void Drive() { }
  }
  </code></pre>
  
  <p><strong>The problem is that I want to add RobotDog class that can Bark and Drive, but not eat.</strong></p>
  
  <hr>
  
  <p>First solution was to create RobotDog as subclass of Robot, </p>
  
  <pre><code>public class RobotDog : Robot
  {
      public void Bark() { Console.WriteLine("Bark"); }
  }
  </code></pre>
  
  <p>but to give it a Bark function we had to copy and paste the Dog's Bark function so now we have duplicate code.</p>
  
  <hr>
  
  <p>The second solution was to create a common super class with a Bark method that then both the Animal and Robot classes inherited from</p>
  
  <pre><code>public class WorldObject
  {
      public void Bark() { Console.WriteLine("Bark"); }
  }
  public class Animal : WorldObject { ... }
  public class Robot : WorldObject { ... }
  </code></pre>
  
  <p>But now EVERY animal and EVERY robot will have a Bark method, which most of them don't need. Continuing with this pattern, the sub-classes will be laden with methods they don't need.</p>
  
  <hr>
  
  <p>The third solution was to create an IBarkable interface for classes that can Bark</p>
  
  <pre><code>public interface IBarkable
  {
      void Bark();
  }
  </code></pre>
  
  <p>And implement it in the Dog and RobotDog classes</p>
  
  <pre><code>public class Dog : Animal, IBarkable
  {
      public void IBarkable.Bark() { Console.WriteLine("Bark"); }
  }
  public class RobotDog : Robot, IBarkable
  {
      public void IBarkable.Bark() { Console.WriteLine("Bark"); }
  }
  </code></pre>
  
  <p>But once again we have duplicate code!</p>
  
  <hr>
  
  <p>The fourth method was to once again use the IBarkable interface, but create a Bark helper class that then each of the Dog and RobotDog interface implementations call into.</p>
  
  <p>This <em>feels</em> like the best method (and what the video seems to recommend), but I could also see a problem from the project getting cluttered with helpers.</p>
  
  <hr>
  
  <p>A fifth suggested (hacky?) solution was to hang an extension method off an empty IBarkable interface, so that if you implement IBarkable, then you can Bark</p>
  
  <pre><code>public interface IBarker {    }
  public static class ExtensionMethods
  {
      public static void Bark(this IBarker barker) { 
          Console.WriteLine("Woof!"); 
      }
  }
  </code></pre>
  
  <hr>
  
  <p>A lot of similar answered questions on this site, as well as articles I've read, seem to recommend using <code>abstract</code> classes, however, wouldn't that have the same issues as solution 2?</p>
  
  <hr>
  
  <p><strong>What is the best object-oriented way to add the RobotDog class to this example?</strong> </p>