title: Java: Getting the subclass from a superclass list
body: <p>i'm new with java and have 2 questions about the following code:</p>
  
  <pre><code>class Animal { }
  class Dog extends Animal { }
  class Cat extends Animal { }
  class Rat extends Animal { }
  
  class Main {
    List&lt;Animal&gt; animals = new ArrayList&lt;Animal&gt;();
  
    public void main(String[] args) {
      animals.add(new Dog());
      animals.add(new Rat());
      animals.add(new Dog());
      animals.add(new Cat());
      animals.add(new Rat());
      animals.add(new Cat());
  
      List&lt;Animal&gt; cats = getCertainAnimals( /*some parameter specifying that i want only the cat instances*/ );
    }
  }
  </code></pre>
  
  <p>1) Is there any way to get either the Dog or Cat instances from the Aminal list?
  2) If yes, how should i correctly build the getCertainAnimals method?</p>