String does not implement AutoCloseable, so how can we assign the result of my method "get" to a String? Does anyone know why this compiles under Java 8 and 11? If I swap AutoCloseable with a non-interface class like Integer, I get the compiler error I expected.
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class FooTest {
@Test
public void myTest() {
final String result = new Foo().get(0);
}
}
class Foo {
List<AutoCloseable> list = new ArrayList<>();
public <A extends AutoCloseable> A get(int name) {
return (A) list.get(name);
}
}