Arraylist of subclass

Dear all,

I have a class called Gene that have two subclasses : Translation and RotationAngle

chromozome is an ArrayList<Gene>

I add in Chromozome instances of Translation and RotationAngle :

ArrayList<Gene> chromozome =new ArrayList<Gene>();

chromozome.add(new Translation());

chromozome.add(new Translation());

chromozome.add(new RotationAngle());

I want then to access them :

Translation t1 = (Translation)chromozome.get(0);

Translation t2 = (Translation)chromozome.get(1);

RotationAngle ra = (RotationAngle)chromozome.get(2);

The last line throws a ClassCastException saying Translation can not be cast to RotationAngle ...

I truly do not get it, since this part of the ArrayList always contained instance of RotationAngle !!!

Is this normal ?

thx,

Vincent

[1005 byte] By [Vebe75a] at [2007-11-27 11:53:42]
# 1

I even specify the indexes :

chromozome.add(0,new Translation());

chromozome.add(1,new Translation());

chromozome.add(2,new RotationAngle());

Vebe75a at 2007-7-29 18:51:46 > top of Java-index,Java Essentials,Java Programming...
# 2

Sorry for the mess.

Gene is not a superclass, but an interface implemented by both RotationAngle and Translation

Vebe75a at 2007-7-29 18:51:46 > top of Java-index,Java Essentials,Java Programming...