The following throws a compile error because foo.getClass() isn’t the same capture group as F extends Foo somehow:
public <F extends Foo> F create (final F foo){
return foo.getClass().cast(foo);
}
The following works fine.
return foo;
Thanks.
Object#getClass()returns aClass<? extends Foo>, not aClass<F extends Foo>.The erasure of
<F extends Foo>is the upper bound,Foo. Therefore theClass#cast()call is roughly equivalent toand not
As you’ve probably figured out by now, a
Foois not anF extends Foo(the declared method return type), so the compiler won’t let you return aFoo.