Not in general.
Suppose Bar
has a method void get(T value)
, and there are two implementations of Foo<String>
, MyFoo
and YourFoo
. Now suppose a caller calls addBar
on a value of type Bar<MyFoo>
. This works: when U
= Foo<String>
, we have that Bar<MyFoo>
is a subtype of Bar<? extends U>
. Now we cast that value to a Bar<Foo<String>>
.
Now if Bar
has no methods that accept T
's as arguments, there's no problem. But suppose it has a method void process(T value)
. The implementation we called has T
= MyFoo
, so it only has a process(MyFoo value)
method. Once we cast it to a Bar<Foo<String>>
, though, we might call it with a YourFoo
instead. This is illegal.
Stab in the dark, but I suspect that what you really wanted to do was declare barFoo
as a List<? extends Bar<? extends Foo<String>>
.