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: 
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...
}

```
You need to have 2 interfaces

```
public interface Beak {
   String getBeakSize();
}

public interface Tail {
   String getTailSize();
}

```

Now your code would look like this

```
public class Animal {

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

}

public class Dog extends Animal implements Tail {
  private String tailLength;

  @Override
  public String getTailLength() { ... }
}

public class Bird extends Animal {
  private String beakSize;

  @Override
  public String getBeakSize() { ... }

}

```

You can add items as you were doing before.

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

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

```

But now, you can use the interfaces to find if the animal has tail or beak or both or nothing at all.

```
for(Animal animal in animals) {
     if(animal instanceof Tail) {
          System.out.println(((Tail)animal).getTailLength());
     }
     if(animal instanceof Beak) {
          System.out.println(((Beak)animal).getBeakLength());
     }
}

```

Why this is important? This way you can have another animal lets say Cat or another bird which also have tail. This piece will work with all of them.