Reputation: 106
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{
}
Upvotes: 2
Views: 100
Reputation: 140427
The above compiles fine with Java8:
class SuperClass { }
class Subclass extends SuperClass{ }
class Test {
public Collection<Class<? extends SuperClass>> testFunction() {
return Collections.singleton(Subclass.class);
}
}
The point is: with Java 8, type inference was heavily reworked and improved.
So my guess here is: this doesn't compile for you because you are using Java 7; where simply spoken the compiler wasn't "good enough" to correctly resolve such kind of code.
Upvotes: 3