I am new to Generics and trying to understand why this code compiles:
public Collection<Class<Subclass>> testFunction() {
return Collections.singleton(Subclass.class);
}
And this code doesn't:
public Collection<Class<? extends SuperClass>> testFunction() {
return Collections.singleton(Subclass.class);
}
My SubClass looks like this:
public class Subclass extends SuperClass{
}
Collections.singleton(Integer.class);should create aCollection<Class<? extends Number>>but if it doesn't (and older compilers had problems here) you'll get a type conflict.eclipsec), but it works fine for mars. You can help out the compiler by providing the explicit type:Collections.<Class<? extends SuperClass>>singleton(Subclass.class);