Consider the following generic converter class (simplified/incomplete for brevity)...
public abstract class BaseConverter <TModel>
{
public void convert(String data, Class<TModel> classOfModelType)
{
}
public void convert(String data)
{
convert(data, TModel.class); // This doesn't work!
}
}
Externally in the subclass I can call the first convert
without issue. Consider this subclass with Car
as the generic type argument. I can pass in Car.class
, like so...
public class CarConverter extends BaseConverter<Car>
{
public void someFunction()
{
String someString;
convert(someString, Car.class);
}
}
What I'm trying to do is move as much as possible to the base class. Since 'Car' is known here and therefore convert
expects Car.class
, and TModel
represents Car
in the base class (for this concrete BaseConverter anyway), I was trying to use TModel.class
in the base class, but as stated, it doesn't work.
So how do you achieve this in Java?