Question: Is it possible to put classes with the same superclass in an ArrayList and that I can retrieve the fields of the subclass?

```
public class Animal {

  private String name; 
  public String getName() { ... }

}

public class Dog extends Animal {

  private String tailLength;
  public String getTailLength() { ... }
}

public class Bird extends Animal {

  private String beakSize;
  public String getBeakSize() { ... }

}

```

Now I'd like to put them in an arrayList

```
private List<Animal> animals = new ArrayList<>();

animals.put(new Dog());
animals.put(new Bird());

```

I'm able successfully put them in the list, but when I retrieve the value from the list, I'm not able to get the beakSize from the bird class and the tailLength from the dog class.

Can you suggest of a strategy how to do this? Thanks!

Answer: 
Why isn't this working?
=======================

You've got list of type `Animal`. This means that the compiler has one and only one guarantee. That this object has the fields and methods associated with the `Animal` superclass. Test this out for yourself.

```
Animal myBird = new Bird();

myBird.getBeakSize(); // This won't compile.

```

The compiler doesn't know it's of type `Bird` because its reference type is `Animal`. 

Different Approach
==================

What you need to do is rethink your class design, such that your superclass has the necessary information for you to access.

If you need access to the specific subclass, then I would recommend keeping separate lists of each subclass, possibly inside of an object. 

An alternative might be something like this:

```
public abstract class Animal {

  private String name; 
  public String getName() { ... }
  public abstract String getFaceDepth(); //

}

public class Bird {
  public String getFaceDepth() {
     // Return beak size here.
  }
}

```

Now, you'll notice here that *face depth* isn't particularly helpful. This is the problem of making general cases. Sometimes, they're so general, they lose the essence of the data they're representing. This can be a sign that your class structure needs to be thought about, depending on your problem.
When you are retrieving elements from array, you can use 'instanceof' method:

```
Animal animal = // get element/iterate over array
if(animal instanceof Dog) {
  Dog dog = (Dog) animal;
  // continue here...
}
if(animal instanceof Bird) {
  Bird bird = (Bird) animal;
  // continue here...
}

```