http://datumedge.blo...-8-lambdas.html
One part of the blog (which seems to be something of a tangent to lambdas) is about providing default implementations for methods in interfaces. Here is what it says:
Quote
Default methods
With Java today, it is not possible to add methods to a published interface without breaking existing implementations. Java 8 gives us a way to specify a default implementation in the interface itself:
Subinterfaces can override a default method:
Or a subinterface can remove the default by redeclaring the method without a body:
Doing this forces an implementation of FastQueue to implement deleteAll().
With Java today, it is not possible to add methods to a published interface without breaking existing implementations. Java 8 gives us a way to specify a default implementation in the interface itself:
interface Queue {
Message read();
void delete(Message message);
void deleteAll() default {
Message message;
while ((message = read()) != null) {
delete(message);
}
}
}
Subinterfaces can override a default method:
interface BatchQueue extends Queue {
void setBatchSize(int batchSize);
void deleteAll() default {
setBatchSize(100);
Queue.super.deleteAll();
}
}
Or a subinterface can remove the default by redeclaring the method without a body:
interface FastQueue extends Queue {
void deleteAll();
}
Doing this forces an implementation of FastQueue to implement deleteAll().
I'm not sure how I feel about this. There have been times where I thought something like this would be useful. I mean, nonsense like MouseAdapter would be obsolete (or could be if they rework the interfaces to define empty default methods). I know these interfaces still can't store state so any methods defined must be expressed in terms of other methods but it does seem to muddy the waters between interfaces and abstract classes.
Right now I don't know what to think. It seems less principled to allow implementations but maybe more practical. I'd be interested to hear what others make of it.

New Topic/Question
Reply



MultiQuote





|