is it possible in Java to narrow down the generic type in a subclass of a generic class? My case looks like this:
class C<T> {
protected T foo() {...}
protected void bar(Supplier<T> foobar) {...}
}
class A<T extends B> extends C<T> {
public void someMethod(){
B b = foo(); // Compiler does not complain
bar(() -> createB()); // Compiler complains with "Type mismatch: cannot convert from B to T"
}
}
Maybe what I did is totally stupid, but I don't get why the compiler does not complain about foo(), but about bar().
Any advice on how to do this, or why not to do this at all, is highly appreciated :)
Kind regards, Thomas