I was following a tutorial about generics in Java defining this static method:
public static <T extends Comparable<T>> T min(T a) { ... }
and saying that
min(new GregorianCalendar());
couldn't compile because GregorianCalendar extends Calendar
and Calendar implements Comparable<Calendar>
so it implied that GregorianCalendar implements Comparable<Calendar>
and NOT Comparable<GregorianCalendar>
.
So in order to compile the signature must be changed into:
public static <T extends Comparable<? super T>> T min(T a) { ... }
which is totally understandable. The 1st version of the method effectively doesn't compile in java-5 but it compiles in java-8! (i tried 5 through 8)
Why java-8 now allows that? (because it makes it more confusing now). What's the new "rule" behind that?