Pregunta

Whu Do it is a non-valid construction

class A <T extends  String & Comparable<T>>{}

out:

java: java.lang.Comparable cannot be inherited with different arguments: <T> and <java.lang.String>

but it is valid

class A <T extends  Number & Comparable<T>>{}

I noiced that it is related with String is final but Number - not.

But T String is valid at first case I think. Why not?

¿Fue útil?

Solución

The difference is, String class already implements Comparable<String>, while Number class doesn't. So, with that bound, T would be implementing both Comparable<String> and Comparable<T>, which is not allowed.

A class cannot extend from or implement different parameterized instantiation of a generic type.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top