Casting Object to another type
I have a problem with type casting!
Code:
public class MyCollection<E>{
protected E[] elements;
protected int top;
public static final int SIZE = 10;
public MyCollection() {
[B]elements = (E[]) new Object[SIZE];[/B]
top = -1;
}
}
...
public class Test {
public void draw(MyCollection<? extends Shape> list){
for( int i = 0 ; i<=list.top ; i++)
[B] list.elements[i].[/B]draw(); // compile error - can't cast Object to Shape
}
public static void main(String[] args) {
MyCollection<Shape> list = new MyCollection<Shape>();
Test test = new Test();
list.push(new Circle()); // extends Shape , push a new Circle to list
list.push(new Square()); // extends Shape
list.push(new Triangle()); // extends Shape
test.draw(list);
}
}
how should i do ?