I have a generic method with generic Map parameter. I want to override it and use concrete subtype, but without cast I am not able to return my subtype
public interface GarrageSimple {
<T extends Car> T getCar(Map<String, T> c);
}
Implementation is below which gives error as: Incompatible types. Required: T Found: Bmw
public class GarrageSimpleImpl implements GarrageSimple {
@Override
public <T extends Car> T getCar(Map<String, T> c) {
return new Bmw();
}
}
Actually, Bmw is a subclass of Car which means a type of T. When I cast it like * return (T)new Bmw();* it works with a warning that Unchecked cast Bmw to T
I know that it is because I take a risk to return concrete subclass while method signature is generic, but how I can change my method to make it concrete and not break override signature? If I can, in that case, it should work as expected.
PS: I don't want to make my class generic, I just need to change my method signature to make it concrete during implementation.
Thanks