In the example below the Car<M>
is defined with generic <M>
and it implements interface Vehicle
which does not have generic in its definition. When casting the object back to Car<M>
after assigning it to Vehicle
,
Car<CarModel> car = (Car) carVehicle;
it shows see this warning (s):
Unchecked assignment: 'com.test.Car' to 'com.test.Car<com.test.CarModel>'
Is there a way to avoid this warning?
public class Main {
public static void main(String[] args) {
Main main = new Main();
Vehicle carVehicle = new Car<>(new CarModel("XM"), 100);
Car<CarModel> car = (Car) carVehicle; // Unchecked assignment: 'com.test.Car' to 'com.test.Car<com.test.CarModel>'
}
}
interface Vehicle {
long getVehicleIdNum();
}
class CarModel {
String model;
CarModel(String model) {
this.model = model;
}
}
class Car<M> implements Vehicle {
M model;
long vin;
Car(M model, long vin) {
this.model = model;
this.vin = vin;
}
@Override
public long getVehicleIdNum() {
return vin;
}
M getModel() {
return model;
}
}