Below is the code where I declared two methods whose return type are bounded parameters but one is based on interface whereas other is based on a class. In the test() method, assigning return type to String fails for method where bounded parameter extends from Class which is expected, but compiler doesn't throw error when the bounded parameter is based on interface. Can you explain why does this happen?
import java.io.FileInputStream;
import java.util.function.Predicate;
public class Dummy
{
<T extends Predicate<String>> T getBoundedByInterface()
{
return null;
}
<T extends FileInputStream> T getBoundedByClass() {
return null;
}
public void test()
{
String compilationError = getBoundedByClass(); //Expected
String works = getBoundedByInterface(); //No compilation error. Why?
}
}