Possible Duplicate:
get type of a generic parameter in java with reflection
Hi I read here about solutions to get instances of generic types. I think my my problem differs a bit, so I break it down for easyness. Lets assume I have three classes:
public class Car (){
public int velocity;
public String name;
public Car(int velocity, String name){
this.velocity = velocity;
this.name = name;
}
}
public class ColoredCar extends Car(){
public String color;
//setters and getters for color, same Constructor....
}
public class DrivenCar extends ColoredCar(){
public Driver driver;
//setters and getters, same Constructor
}
Now I extended the ArrayList like:
public class Cars<CAR extends Car> extends ArrayList<CAR>{
public String[] getColors(){
String[] ret = new String[this.size()];
int count = 0;
for(CAR c: this){
ret[count++] = c.getColor();
}
return ret;
}
//some more.....
}
ok, If I need to create a class which extends Cars
and in its Constructor I know already they are 10 Objects of CAR
, what do I have as possibility? I know I can create Car Objects with velocity and name, which I have.
Edit: lets say I have a function in my Cars:
public CAR createCar(int velocity, String name){
return (CAR)new Car(velocity, name);
}
and at Runtime Cars is defined as Cars, do I get an error, because I can't cast a Car to a DrivenCar?