I have a generic class MyClass<T>
with a static factory method and a setter method:
public class MyClass<T> {
public static <T> MyClass<T> with(Context context) {
MyClass<T> myClass = new MyClass<>();
myClass.context = context;
return myClass;
}
public void setClass(Class<? extends CustomClass<T>> customClass) {
...
}
...
}
With the setter method, user can set any class that extends from "CustomClass".
So far so good. If I do:
MyClass<String> myClass = MyClass.with(this);
myClass.setClass(MyCustomClass.class);
It works perfectly. But if I do:
MyClass.with(this).setClass(MyCustomClass.class);
It does not compile! The compiler outputs:
Error:(44, 87) error: incompatible types: Class<MyCustomClass> cannot be converted to Class<? extends MyCustomClass<Object>>
I don't know why it wont compile with the second option. MyCustomClass
is like:
public class MyCustomClass extends CustomClass<String>