title: Is there anyway to make this work without explicit casting? (trying a Generic Parent-Child relationship)
body: <p><strong>The Interfaces:</strong></p>
  
  <pre><code>package sandBox.ps.generics.compositePattern;
  
  import java.util.Collection;
  
  interface AnimalInterface {
      String getID();
  
      /*
       * getAnimals can be:
       * 1. AnimalInterface
       * 2. Anything that implements/extends AnimalInterface (i.e. AnimalInterface or DogInterface)
       */
      Collection&lt;? extends AnimalInterface&gt; getAnimals();
  }
  
  interface DogInterface extends AnimalInterface {
      String getBreed();
  }
  </code></pre>
  
  <p><strong>The classes:</strong></p>
  
  <pre><code>package sandBox.ps.generics.compositePattern;
  
  import java.util.Collection;
  import java.util.Collections;
  
  class AnimalClass implements AnimalInterface {
      private final String id;
      private final Collection&lt;? extends AnimalInterface&gt; animals;
  
      AnimalClass(final String id,
          final Collection&lt;? extends AnimalInterface&gt; animals) {
      this.id = id;
      this.animals = animals;
      }
  
      @Override
      public String getID() {
      return this.id;
      }
  
      @Override
      public Collection&lt;? extends AnimalInterface&gt; getAnimals() {
      return this.animals;
      }
  }
  
  class DogClass extends AnimalClass implements DogInterface {
  
      private final String breed;
  
      DogClass(final String id, final String breed) {
      super(id, Collections.&lt;AnimalInterface&gt; emptyList());
      this.breed = breed;
  
      }
  
      @Override
      public String getBreed() {
      return this.breed;
      }
  
  }
  </code></pre>
  
  <p><strong>Testing the classes:</strong></p>
  
  <pre><code>package sandBox.ps.generics.compositePattern;
  
  import java.util.ArrayList;
  import java.util.Collection;
  
  public class TestClass {
      public void testA() {
      // Dog Collection (Child)
      final DogInterface dog = new DogClass("1", "Poodle");
      final Collection&lt;DogInterface&gt; dogCol = new ArrayList&lt;DogInterface&gt;();
      dogCol.add(dog);
  
      // Animal Collection of Dogs (Parent)
      final AnimalInterface animal = new AnimalClass("11", dogCol);
      final Collection&lt;AnimalInterface&gt; parent = new ArrayList&lt;AnimalInterface&gt;();
      parent.add(animal);
      // Animal Collection of Animals (Grand-Parent)
      final AnimalInterface grandParent = new AnimalClass("11", parent);
  
      // Get Dog
      for (final AnimalInterface parents : grandParent.getAnimals()) {
          /* I know the this code would work.
           * My question is, is there anyway to do this without explicit casting
          for (final AnimalInterface child : parents.getAnimals()) {
      if (child instanceof DogInterface) {
          System.out.println(((DogInterface)child).getBreed());
      }
      }
          */
  
          /*  HERE:   This is the part I am trying to solve.
           *  Do I use extends or super for AnimalInterface
           *  Is there any option such that I don't need to do a casting of some type
           */
          for (final DogInterface child : parents.getAnimals()) {
          System.out.println(child.getBreed());
          }       
      }
      }
  }
  </code></pre>
  
  <p>Questions:   </p>
  
  <ol>
  <li>The last lines of the test class try to access the animals.</li>
  <li>What I am trying to figure out is that, is there anyway to avoid
  explicit casting?</li>
  <li>Is there any combination of extends, super or some other generic
  term that would make this work?</li>
  <li>If casting is the only option where should it be done?</li>
  <li>I already know this would work:</li>
  </ol>
  
  <blockquote>
  <pre><code>for (final AnimalInterface child : parents.getAnimals()) {
      if (child instanceof DogInterface) {
         System.out.println(((DogInterface)child).getBreed());
      }
  }
  </code></pre>
  </blockquote>