https://blog.jooq.org/2014/04/04/java-8-friday-the-dark-side-of-java-8/
Overloading gets even worse
Not all keywords are supported on default methods
The key goal of adding default methods to Java was “interface evolution”, not “poor man’s traits.”
Not all keywords are supported on default methods
They cannot be made final
They cannot be made synchronized
I don’t think #3 is an option because interfaces with method bodies are unnatural to begin with. At least specifying the “default” keyword gives the reader some context why the language allows a method body. Personally, I wish interfaces would remain as pure contracts (without implementation), but I don’t know of a better option to evolve interfaces.
http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/
Overloading gets even worse
Not all keywords are supported on default methods
The key goal of adding default methods to Java was “interface evolution”, not “poor man’s traits.”
Not all keywords are supported on default methods
They cannot be made final
They cannot be made synchronized
I don’t think #3 is an option because interfaces with method bodies are unnatural to begin with. At least specifying the “default” keyword gives the reader some context why the language allows a method body. Personally, I wish interfaces would remain as pure contracts (without implementation), but I don’t know of a better option to evolve interfaces.
http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/
Suppose Java 8 is out and has lambdas. Now you would like to start using lambdas and the most obvious use case for that is to apply a lambda to every element of a collection.
List<?> list = …
list.forEach(…); // lambda code goes here
The
forEach
isn’t declared by java.util.List
nor the java.util.Collection
interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation.public interface A {
default void foo(){
System.out.println("Calling A.foo()");
}
}
public interface B {
default void foo(){
System.out.println("Calling B.foo()");
}
}
public class Clazz implements A, B {
}
This code fails to compile with the following result:
java: class Clazz inherits unrelated defaults for foo() from types A and B
public class Clazz implements A, B {
public void foo(){
A.super.foo();
}
}
@FunctionalInterface
public interface Iterable<T> {
Iterator<T> iterator();
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
}
http://www.programcreek.com/2014/12/default-methods-in-java-8-and-multiple-inheritance/