title: Access a subclass method through an ArrayList of superclass objects?
body: <p>I have a superclass(Animal) and two subclasses(Frog and Lion). In my private method below, I'm creating three Lions and one Frog. I am displaying all four of the Animal's weight in my first loop, and all of the Lion's names in the second loop. I get the correct output, but my teacher told me "there is no need for creating two ArrayLists in your main method." I'm guessing he is suggesting that I use my first ArrayList to access the getName() method of my Lion Subclass.</p>
  
  <p>This is how i did it and it was working:</p>
  
  <pre><code> import java.util.ArrayList;
  
      public class AnimalTest {
  
          public static void main(String[] args){
              ArrayList&lt;Animal&gt; animalList = createList();
  
              for(int i = 0; i &lt; animalList.size(); i++){
                  System.out.println(animalList.get(i).getWeight() + " lbs");
              } 
  
              ArrayList&lt;Lion&gt; lionList = createList();
              for(int j = 0; j &lt; 3; j++){
                  System.out.println(lionList.get(j).getName());
              }
  
          }
  
          private static ArrayList createList(){
              ArrayList&lt;Animal&gt; list = new ArrayList&lt;Animal&gt;();
  
              list.add(new Lion("Leo I", 530.00));
              list.add(new Lion("Leo II", 560.00));
              list.add(new Lion("Leo III", 590.00));
              list.add(new Frog("Freddie", 7.00));
  
              return list;
          }
  
      }
  </code></pre>
  
  <p>Does he want me to access my getName() function from my Lion class through the same ArrayList list? I was thinking I could do this with some casting, like so:</p>
  
  <pre><code>//ArrayList&lt;Lion&gt; lionList = createList();
  for(int j = 0; j &lt; 3; j++){
     System.out.println(animalList.(Lion)get(j).getName());
  }
  </code></pre>
  
  <p>But this doesn't work.. I don't understand how I'm supposed to get to my Lion subclass's getName() method without creating a another ArrayList..</p>
  
  <p>Here are the rest of my classes:</p>
  
  <pre><code>public class Animal {
      private String genus;
      private String species;
      private double weight;
      private boolean tail;
  
      public Animal(){}
  
      public Animal(String genus, String species){
          this.genus = genus;
          this.species = species;
      }
  
      public String getGenus(){
          return genus;
      }
  
      public String getSpecies(){
          return species;
      }
  
      public double getWeight(){
          return weight;
      }
  
      public void setWeight(double weight){
          this.weight = weight;
      }
  
      public boolean hasTail(){
          return tail;
      }
  
      public void setTail(boolean tail){
          this.tail = tail;
      }
  
  }
  
  
  public class Lion extends Animal {
      private String name;
  
      public Lion(){}
  
      public Lion(String name, double weight){
          super("Panthera", "Leo");
          this.name = name;
          setWeight(weight);
          setTail(true);
      }
  
      public String getName(){
          return name;
      }
  }
  
  
  public class Frog extends Animal {
      private String name;
  
      public Frog(){}
  
      public Frog(String name, double weight){
          super("Lithobates", "Catesbeianus");
          this.name = name;
          setWeight(weight);
          setTail(false);
      }
  
      public String getName(){
          return name;
      }
  }
  </code></pre>