Reputation: 651
It error's on the "return this;" line with this: Type mismatch: cannot convert from A to T.
public class A<T extends A<T>>{
public T method() {
return this;
}
}
I don't understand why the compiler can't convert from A to T, when A is a suitable candidate for T.
Upvotes: 2
Views: 1081
Reputation: 7057
Let us assume your code was valid...
You might have a subclass defined like this
public class B extends A<B> {
}
Here type parameter T
is same as type of this
.
Which is why I guess you said
A is a suitable candidate for T
Now consider this case
public class C extends A<B> {
}
which is perfectly valid since B
passes all criteria required for T
.
In this case your method declaration in class A
becomes invalid.
(return type is B
but you are returning instance of class C
.)
So your assumption that
A is a suitable candidate for T
is invalid. Hence the error.
Hope this helps.
Good luck.
Upvotes: 2