0

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?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • You didn't get the first error message from the first piece of code. Did it really say `java.lang.Integer`? – user207421 Mar 11 '14 at 05:36
  • possible duplicate of [BaseFoo cannot be inherited with different arguments: > and >](http://stackoverflow.com/questions/19436415/basefoo-cannot-be-inherited-with-different-arguments-t-x-bart-and-t-x-foo) – Makoto Mar 11 '14 at 05:37
  • @EJP I am corrected mistake. For integer similar behaviour – gstackoverflow Mar 11 '14 at 05:37

1 Answers1

6

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525