title: Storing objects of different classes inside an ArrayList
body: <p>I have a abstract animal class and other subclass like reptiles which is further inherited.<br>
  I have created array to initialize the animal as shown below:</p>
  
  <pre><code>public void initializeArray()
  {
      zooAnimal = new Animal[10];         // We will make 10 animals:
  
      // Polymorphism allows us to assign an object of a subclass to an 
      // object reference to a superclass/ancestor.
      zooAnimal[0] = new Kookaburra("Kelly",5);       // A 5kg kookaburra
      zooAnimal[1] = new Lizard("Lizzy",2,3);         // A 2kg, 3-year-old lizard
      zooAnimal[2] = new Crocodile("Colin", 200, 7);      // a 7-yo 200kg croc.
      zooAnimal[3] = new Rosella("Katie", 2, "Crimson");      // a 2-yo    Crimson Rosella
      zooAnimal[4] = new Rosella("Rosie", 4, "Green");        // a 4-yo Green Rosella
      zooAnimal[5] = new Snake("Boris","Brown",15,3); // A Brown Snake, 15kg, 3 years
      zooAnimal[7] = new Snake("Rita","Rattle",22,1); // A Rattle Snake, 22kg, 1 years
      zooAnimal[6] = new Dolphin("Dolly", 142, 6);    // A heavy, 6-yo dolphin.
      zooAnimal[8] = new Kookaburra("Kenneth",4);     // A 4kg kookaburra
      zooAnimal[9] = new Rosella("Yippy", 1, "Yellow");       // a 1-yo Yellow Rosella
  }
  </code></pre>
  
  <p>But I want to achieve the same <em>using an ArrayList</em> instead of array.<br>
  How this can be done?</p>
  
  <p>My Animal class and sub-classes look like this:</p>
  
  <p><strong>Animal class</strong></p>
  
  <pre><code>public abstract class Animal
  {
      private int weight;
      private int age;
      private String name;
  
      protected Animal(String name, int weight, int age)
      {
          this.name = name;
          this.weight = weight;
          this.age = age;
      }
  
      public final int getWeight() { return weight; }
  
      public final int getAge() { return age; }
  
      public final String getName() { return name; }
  
      public abstract void makeNoise();       // Must be implemented by a subclass
  
      /** Provides a default description of the animal.
      * Sub-classes should override. */
      public String toString()
      {
          return "Animal Object: [name=" + name + ", age=" + age + ", weight=" + weight + "]";
      }
  }
  </code></pre>
  
  <p>And I have a <code>Bird</code> class (sub-class of <code>Animal</code> class), a <code>Kookabura</code> class (sub-class of <code>Animal</code>), <code>Reptile</code> class (sub-class of <code>Animal</code> class) and a <code>Lizard</code> subclass (sub-class of <code>Reptile</code> class) and so on!!</p>