Java8: Why is it forbidden to define a default method for a method from java.lang.Object

This is yet another of those language design issues that seems “obviously a good idea” until you start digging and you realize that its actually a bad idea. This mail has a lot on the subject (and on other subjects too.) There were several design forces that converged to bring us to the current design: … Read more

What is the reason why “synchronized” is not allowed in Java 8 interface methods?

While at first it might seem obvious that one would want to support the synchronized modifier on default methods, it turns out that doing so would be dangerous, and so was prohibited. Synchronized methods are a shorthand for a method which behaves as if the entire body is enclosed in a synchronized block whose lock … Read more

Explicitly calling a default method in Java

As per this article you access default method in interface A using A.super.foo(); This could be used as follows (assuming interfaces A and C both have default methods foo()) public class ChildClass implements A, C { @Override public void foo() { //you could completely override the default implementations doSomethingElse(); //or manage conflicts between the same … Read more

Why is “final” not allowed in Java 8 interface methods?

This question is, to some degree, related to What is the reason why “synchronized” is not allowed in Java 8 interface methods? The key thing to understand about default methods is that the primary design goal is interface evolution, not “turn interfaces into (mediocre) traits”. While there’s some overlap between the two, and we tried … Read more

When to use: Java 8+ interface default method, vs. abstract method

There’s a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default) method in the interface. The constraint on the default method is that it can be implemented only in the terms … Read more