I have a list with a syntax similar to this:
List<? extends MyInterface> list = new ArrayList<>();
I have an object that implements MyInterface
, but using the above code, I am unable to add that object to the list:
Object object = getMyObject(); // I can only receive it here as Object
list.add((MyImplementation)object);
The following doesn't work either:
list.add((MyInterface)object);
Can anyone please point out what am I doing wrong here?