Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Note that Comparable is also a generic type, parameterized by what type it is comparable to. The most general, type-safe way to declare your mergeSort function above is:</p> <pre><code>private &lt;T extends Comparable&lt;? super T&gt;&gt; LList&lt;T&gt; mergeSort(LList&lt;T&gt; list) { } </code></pre> <p>This enforces that the type T's compareTo method can accept an argument of type T. (In theory, a type could implement Comparable, but not be comparable to itself, like <code>SomeClass implements Comparable&lt;CompletelyDifferentClass&gt;</code>, so it is important to have the requirement on the type parameter of Comparable. In practice, however, any well-designed Comparable class should be comparable to at least itself.)</p> <p>We require that <code>&lt;T extends Comparable&lt;? super T&gt;&gt;</code> instead of just <code>&lt;T extends Comparable&lt;T&gt;&gt;</code> because it is okay if a type T's <code>compareTo</code> method accepts a more general type than T, because it would still be able to accept an argument of type T. This is important because, if you have a class A, which implements <code>Comparable&lt;A&gt;</code>; and then you have a subclass B which extends A, B cannot implement <code>Comparable&lt;B&gt;</code>, because B already implements <code>Comparable&lt;A&gt;</code>, inherited from A, and a class cannot implement an interface twice. So if we required <code>&lt;T extends Comparable&lt;T&gt;&gt;</code> above, B would not satisfy it, and we would not be able to sort <code>LList&lt;B&gt;</code> objects.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload