Why does the following code give a compile error?
public MyObject(Builder<? extends MyObject> builder) {
// Type mismatch: cannot convert from MyObject.Builder<capture#5-of ? extends MyObject> to MyObject.Builder<MyObject>
Builder<MyObject> myObjBuilder = builder;
}
If the Builder type is a subclass of MyObject, then why can't you assign builder to just type MyObject? I need to do this because I am unable to use an object of type MyObject with the builder. Take a look at this code for example:
public MyObject(Builder<? extends MyObject> builder) {
// The method getData(capture#8-of ? extends MyObject) in the type Builder<capture#8-of ? extends MyObject> is not applicable for the arguments (MyObject)
this.data = builder.getData(this);
}
I feel like this should be allowed. Or am I missing something here? Is there a way to do this without casting builder to (Builder<MyObject>)
and having to use @SuppressWarnings ?
Also note that I need Builder to be <? extends MyObject>
because the MyObject and its Builder will be subclassed (as it is abstract).
Thanks for your help!