Java Generics - Cannot convert from <? extends MyObject> to <MyObject>

User12345

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!

Willem Van Onsem

Because Foo<? extends Bar> is not a Foo<Bar>.

Say Foo has a method:

void add (T t)

then by contract, you can only add T objects. Now if T is instantiated as ? extends Bar, we don't know the type. Accepting Bar could lead to problematic behavior: if you have an ArrayList<Foo> you expect the ArrayList to contain only Foo instances. If you would see the ArrayList<Foo> as a ArrayList<SuperFoo>, one can't guarantee that the ArrayList<Foo> contains only Foo's.

Some languages enable covariance: if you only use T as output, you can say that a class Foo<T> is a the same as Foo<SuperT> as well (the same with input). But currently Java doesn't support that. C# however does on the interface level.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting Cannot convert from Set<MyObject> to Set<MyObject<?,?>>

From Dev

Java generics: cannot convert <X> into <? extends X>

From Dev

Cannot cast from ArrayList<Object> to ArrayList<MyObject>

From Dev

Cannot await Task<MyObject>

From Dev

Convert IObservable<MyObject> into IObservable<bool>

From Dev

Convert an IFile (JSON File) to MyObject

From Dev

How to convert a Spark RDD[Array[MyObject]] into RDD[MyObject]

From Dev

How to convert a Spark RDD[Array[MyObject]] into RDD[MyObject]

From Dev

C# Convert nested groups to List<MyObject>

From Dev

Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D>

From Dev

$this->myObject = clone $this->myObject;

From Dev

$this->myObject = clone $this->myObject;

From Dev

Convert List<MyObject> to List<List<String>> using java8 lambdas only

From Dev

Java Generics error: Cannot convert from E to E?

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Java generics : Type mismatch: cannot convert from Integer to K

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Java generics extends syntax

From Dev

PHP Array of Objects :: Cannot use object of type <MyObject> as array

From Dev

How to generate checkboxes from List<MyObject> with <s:checkboxlist /> tag?

From Dev

How do I get all data from firebase into list<myObject>

From Dev

ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

From Dev

Add conversion service String to MyObject using java notation (Spring + Hibernate)

From Dev

ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

From Dev

ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

From Dev

typescript MyObject.instanceOf()

From Dev

Drag MyObject to JTable (DnD)

From Dev

Send myObject by TCP/IP

From Dev

Java Generics cannot convert Type in Type

Related Related

  1. 1

    Getting Cannot convert from Set<MyObject> to Set<MyObject<?,?>>

  2. 2

    Java generics: cannot convert <X> into <? extends X>

  3. 3

    Cannot cast from ArrayList<Object> to ArrayList<MyObject>

  4. 4

    Cannot await Task<MyObject>

  5. 5

    Convert IObservable<MyObject> into IObservable<bool>

  6. 6

    Convert an IFile (JSON File) to MyObject

  7. 7

    How to convert a Spark RDD[Array[MyObject]] into RDD[MyObject]

  8. 8

    How to convert a Spark RDD[Array[MyObject]] into RDD[MyObject]

  9. 9

    C# Convert nested groups to List<MyObject>

  10. 10

    Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D>

  11. 11

    $this->myObject = clone $this->myObject;

  12. 12

    $this->myObject = clone $this->myObject;

  13. 13

    Convert List<MyObject> to List<List<String>> using java8 lambdas only

  14. 14

    Java Generics error: Cannot convert from E to E?

  15. 15

    Java generics. Type mismatch: cannot convert from object to

  16. 16

    Java generics : Type mismatch: cannot convert from Integer to K

  17. 17

    Java generics. Type mismatch: cannot convert from object to

  18. 18

    Java generics extends syntax

  19. 19

    PHP Array of Objects :: Cannot use object of type <MyObject> as array

  20. 20

    How to generate checkboxes from List<MyObject> with <s:checkboxlist /> tag?

  21. 21

    How do I get all data from firebase into list<myObject>

  22. 22

    ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

  23. 23

    Add conversion service String to MyObject using java notation (Spring + Hibernate)

  24. 24

    ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

  25. 25

    ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

  26. 26

    typescript MyObject.instanceOf()

  27. 27

    Drag MyObject to JTable (DnD)

  28. 28

    Send myObject by TCP/IP

  29. 29

    Java Generics cannot convert Type in Type

HotTag

Archive