Generics oddness

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue May 29 08:40:12 PDT 2012


Hi Ben,
the problem is that there is a clash  between two methods that are 
not-override equivalent; one is

class Comparator {
[...]
     Comparator<T> reverse() default {
         return Collections.reverseOrder(this);
     }
[...]
}


The other one is:

class Ordering<T> implements Comparator<T> {
     public <S extends T> Ordering<S> reverse() {
         // ...
     }
}

As per JLS, this is an illegal override, as Ordering.reverse is not a  
subsignature of Comparator.reverse (as the number of type parameter 
differs). However, I'm investigating as to why the compiler is not 
reporting a clash (as it should).

Thanks
Maurizio

On 29/05/12 16:07, Ben Evans wrote:
> public class Ordering<T>  implements Comparator<T>  {
>      @Override
>      public int compare(T o1, T o2) {
>          // ...
>      }
>
>      public<S extends T>  Ordering<S>  reverse() {
>          // ...
>      }
> }
>
> public class OrderingMain<T>  {
>          Ordering<T>  myOrdering;
>
>          public Ordering<T>  get() {
>              return myOrdering.reverse();
>          }
> }



More information about the lambda-dev mailing list