The interface Delayed requires any
implementation of this interface [to] define a compareTo method that provides an ordering consistent with its getDelay method.
However I'm wondering, why there isn't a default implementation in Java 8, as compareTo
is required by contract to depend solely on getDelay
.
Is there a specific reason why this is left for the implementing class? Or is it impossible to create a default method when overwriting a super interface?
Edit: To make my question more understandable, here is an example:
interface Delayed extends Comparable<Delayed> {
long getDelay(TimeUnit unit);
@Override
default int compareTo(Delayed o) {
// might not be the perfect "compareTo" implementation, but you get the point
return o == this? 0:
Long.compare(this.getDelay(TimeUnit.NANOSECONDS), o.getDelay(TimeUnit.NANOSECONDS);
}
}