title: Attribute Inheritance with Interfaces and Abstract Classes - Java
body: <p>Alright, so I'm trying to figure out the best structure for this. I would like to know what would be considered the best practice for these situations and the overall most effective.</p>
  
  <h1>The Problem</h1>
  
  <p>Let's say I'm creating a little world. This world consists of different types of birds, So I create a Bird class to act as a parent for all birds.</p>
  
  <pre><code>public abstract class Bird {
    //stuff all birds have in common
  }
  </code></pre>
  
  <p>Now I want to create different types of birds. Let's create a Penguin and a Goose class and have them extend Bird.</p>
  
  <pre><code>public class Penguin extends Bird{
      //penguin stuff
  }
  
  public class Goose extends Bird{
      //goose stuff
  }
  </code></pre>
  
  <p>So far so good, but now for the wrench. Let's give birds the ability to fly. <em>Surprise</em>, not all birds can fly - such as the penguins! So let's approach our options..</p>
  
  <p>Making a fly method inside of the Bird class is out of the option (not all birds fly). Another smelly way is having multiple parent classes, such as BirdsThatFly, because this would become an enormous mess, especially when other <em>birdly</em> attributes are added into the mix. </p>
  
  <h1>Option 1</h1>
  
  <h2>Interfaces</h2>
  
  <p>One viable option that in theory sounds like the right idea would be to use interfaces. For example, I could have a Fly interface.</p>
  
  <pre><code>public interface Fly {
      public void fly();
  }
  </code></pre>
  
  <p>This would allow me to implement Fly on only Bird child classes that can fly. Example..</p>
  
  <pre><code>public class Goose extends Bird implements Fly {
      @Override
      public void fly() {
          //fly!
      }
  }
  </code></pre>
  
  <p>This appears clean and forces a fly method onto the goose, and seems it could work really well when adding other attributes.</p>
  
  <p>The thing that seems wrong, is that I would still have to create a custom fly for each type of bird, and there could be loads of birds! Would default voids help me here? I feel they could be fairly limiting as these attributes grow. </p>
  
  <p>Perhaps I can be told otherwise and how to rethink this interface structure. </p>
  
  <h1>Option 2</h1>
  
  <h2>My Own Attribute Class</h2>
  
  <p>This involves creating attribute objects and adding them to the individual bird classes. If I'm correct with interfaces, this option may be a valid approach.</p>
  
  <p>The attribute class would look something like this</p>
  
  <pre><code>public abstract class Attribute {
      public abstract void execute();
  }
  </code></pre>
  
  <p>And an attribute could look like this</p>
  
  <pre><code>public class Fly extends Attribute{
      @Override
      public void execute() {
          //fly!
      }
  }
  </code></pre>
  
  <p>This fly attribute can now be added to a bird by having an attribute list in Bird, and populating it in the bird type classes.</p>
  
  <pre><code>public abstract class Bird {
      private List&lt;Attribute&gt; attributes = new ArrayList&lt;Attribute&gt;();
  
      protected void addAttribute(Attribute attribute){
          attributes.add(attribute);
      }
      public List&lt;Attribute&gt; getAttributes(){
          return attributes;
      }
  }
  </code></pre>
  
  <p>And adding it to a Goose...</p>
  
  <pre><code>public class Goose extends Bird{
      public Goose(){
          super.addAttribute(new Fly());
      }
  }
  </code></pre>
  
  <p>This approach to me seems very customizable, but also seems out of practice and maybe not immediately be clear what each class is able to do. It also requires a lot of work to properly set up on larger scale to be able to execute specific attributes.</p>
  
  <p>Is there a way that would be considered better practice, or are either of these a reasonable approach or a good start? </p>