Subclass in ArrayList<Superclass> method calling issues
<p>I have an arraylist of entities. The Entity class has </p>

<pre><code>public void update(int delta, Level level, ArrayList&lt;Entity&gt; entities){
    //do stuff
}
</code></pre>

<p>a subclass of entity, Player, has a function overriding it</p>

<pre><code>public void update(int delta, Level level, ArrayList&lt;Entity&gt; entities){
    //do player specific stuff
    super.update(delta, level, entities);
}
</code></pre>

<p>when I call the player objects update function from an arraylist of entities, like this:</p>

<pre><code>for(Entity entity : entities)            //contains an object of Player
    entity.update(delta, level, entities);
</code></pre>

<p>it calls the entity function directly and the player specific stuff doesnt happen, however if I call it like this:</p>

<pre><code>((Player)entities.get(0)).update(delta, level, entities);
</code></pre>

<p>it does. I thought I understood the problem but I tried the following code</p>

<pre><code>private static class Foo{
    public void print(){
        System.out.println("I AM FOO");
    }
}
private static class Bar extends Foo{
    public void print(){
        System.out.println("i am bar");
        super.print();
    }
}
public static void main(String[] args){
    ArrayList&lt;Foo&gt; foos = new ArrayList&lt;Foo&gt;();
    foos.add(new Foo());
    foos.add(new Bar());
    for(Foo leFoo: foos)
        leFoo.print();
}
</code></pre>

<p>and it actually calls the print function from Bar when the object is a bar. I'm kinda lost at this point.</p>
