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

One of the most useful features of Java 8 are the new default methods on interfaces. There are essentially two reasons (there may be others) why they have been introduced:

  • Providing actual default implementations. Example: Iterator.remove()
  • Allowing for JDK API evolution. Example: Iterable.forEach()

From an API designer’s perspective, I would have liked to be able to use other modifiers on interface methods, e.g. final. This would be useful when adding convenience methods, preventing “accidental” overrides in implementing classes:

interface Sender {

    // Convenience method to send an empty message
    default final void send() {
        send(null);
    }

    // Implementations should only implement this method
    void send(String message);
}

The above is already common practice if Sender were a class:

abstract class Sender {

    // Convenience method to send an empty message
    final void send() {
        send(null);
    }

    // Implementations should only implement this method
    abstract void send(String message);
}

Now, default and final are obviously contradicting keywords, but the default keyword itself would not have been strictly required, so I’m assuming that this contradiction is deliberate, to reflect the subtle differences between “class methods with body” (just methods) and “interface methods with body” (default methods), i.e. differences which I have not yet understood.

At some point of time, support for modifiers like static and final on interface methods was not yet fully explored, citing Brian Goetz:

The other part is how far we’re going to go to support class-building
tools in interfaces, such as final methods, private methods, protected
methods, static methods, etc. The answer is: we don’t know yet

Since that time in late 2011, obviously, support for static methods in interfaces was added. Clearly, this added a lot of value to the JDK libraries themselves, such as with Comparator.comparing().

Question:

What is the reason final (and also static final) never made it to Java 8 interfaces?

5 Answers
5

Leave a Comment