title: Java - For Each Derived class in a Collection of Base classes
body: <p>I have successfully done this with Visual Basic, I just want to know if there is a simple way to do this with Java.</p>
  
  <p>I want a For loop that will iterate through Derived classes from a Base class Collection.</p>
  
  <pre><code>public class Game{
      private Collection&lt;GameEntity&gt; entities;
  
      // Methods would go here to control GameEntity's
  
      private abstract class GameEntity{
          protected Game owner; //Defines which instance Game owns this Entity
  
          public GameEntity(Game owner) {
              this.owner=owner;
          }
  
          public abstract void tick();
      }
  
      private class Partical extends GameEntity{
          public Partical(Game owner) {
              super(owner);
          }
  
          public void tick(){
              for (Partical p : owner.entities) { //HERE IS MY PROBLEM
                  if(p != this){ //Don't do physics with itself
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Alright so here I have my Game class that has a Collection of GameEntity's but GameEntity is strickly a Base class.</p>
  
  <p>Partical is derived from the GameEntity class.</p>
  
  <p>What I want the for loop to do is iterate through the Collection of GameEntity's and pick out all the Partical classes from Collection.</p>
  
  <p>Where my problem is I get an "Incompatible Types" error. I do realize they are different types.</p>