When is an interface with a default method initialized?

This is a very interesting issue! It seems like JLS section 12.4.1 ought to cover this definitively. However, the behavior of Oracle JDK and OpenJDK (javac and HotSpot) differs from what’s specified here. In particular, the Example 12.4.1-3 from this section covers interface initialization. The example as follows: interface I { int i = 1, … Read more

Do Java 8 default methods break source compatibility?

Doesn’t JDK 1.8 introduce a forward incompatibility for Java source code due to default methods? Any new method in a superclass or interface can break compatibility. Default methods make it less likely that a change in an interface will break compatibility. In the sense that default methods open the door to adding methods to interfaces, … Read more

Super class method and Interface default method conflict resolution

Your assumption is right, the concrete method inherited from the superclass takes precedence over the default method from the interface: JLS §8.4.8. Inheritance, Overriding, and Hiding A class C inherits from its direct superclass and direct superinterfaces all abstract and default (§9.4) methods m for which all of the following are true: … No method … 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

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

Purpose of Default or Defender methods in Java 8

Besides having the possibility of adding methods to the interface in future versions, there is the important point of allowing an interface to stay a functional interface even if it has more than one method. A functional interface has only one non-default abstract method which can be implemented via a lambda expression. One example is … 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

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

tech