"final" default method?

7 views
Skip to first unread message

zhong...@gmail.com

unread,
Sep 21, 2015, 12:38:32 PM9/21/15
to java.lang.fans
Is it possible to forbid a default interface method from being overridden by subtypes?

Zhong

y s

unread,
Sep 21, 2015, 2:01:37 PM9/21/15
to Zhong Yu, java.lang.fans
No.

The modifiers are specified in JLS 9.4:
    InterfaceMethodDeclaration:
    InterfaceMethodModifier:
    (one of) 
    Annotation public 
    abstract default static strictfp

(Note the lack of "final"). This is in contrast to method modifiers in classes, which are defined in JLS 8.4.3:
    MethodModifier:
    (one of) 
    Annotation public protected private 
    abstract static final synchronized native strictfp
There was a relevant discussion a few years back, in which Brian Goetz seemed to indicate that this isn't out of the question for the future, but that it wasn't a priority to add them yet. Remember that the primary motivation for default methods has always been interface evolution -- that is, the ability to add methods to an interface without forcing every implementing class to react to those changes -- rather than trait-like behavior.

On Mon, Sep 21, 2015 at 12:38 PM <zhong...@gmail.com> wrote:
Is it possible to forbid a default interface method from being overridden by subtypes?

Zhong

--
You received this message because you are subscribed to the Google Groups "java.lang.fans" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-lang-fan...@googlegroups.com.
To post to this group, send email to java-la...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/java-lang-fans/6e5e83e6-b755-4838-a812-9a2de2b44456%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zhong Yu

unread,
Sep 21, 2015, 2:47:44 PM9/21/15
to y s, java.lang.fans

On Mon, Sep 21, 2015 at 1:01 PM, y s <eleus...@gmail.com> wrote:
No.

The modifiers are specified in JLS 9.4:
 

I was thinking that if the method signature references some package/private accessible types, it will prevent subtypes from overriding. However this is pretty ugly, and I wonder if there could be a better solution. (for curiosity; I have no use case that needs this:)

Zhong Yu

y s

unread,
Sep 21, 2015, 3:07:44 PM9/21/15
to Zhong Yu, java.lang.fans
"Every method declaration in the body of an interface is implicitly public (§6.6)." (also from 9.4).

That said, this limitation was definitely due to time constraints, not for any "religious" reason (see http://stackoverflow.com/a/29338236/1076640). Private interface methods will be in Java 9 (http://mail.openjdk.java.net/pipermail/jdk9-dev/2015-March/001981.html), and presumably will be inherently final, as all private methods today are (I say "presumably" because I haven't actually followed the feature other than knowing that it exists, but I'd be pretty shocked if it weren't).

Iirc, one of the difficulties with private methods in interfaces for Java 8 was the final-ness, so it could be that the same work to get private methods will also get you final default methods.

zhong...@gmail.com

unread,
Sep 21, 2015, 3:19:44 PM9/21/15
to java.lang.fans


On Monday, September 21, 2015 at 2:07:44 PM UTC-5, Yuval Shavit wrote:
"Every method declaration in the body of an interface is implicitly public (§6.6)." (also from 9.4).



Let say my method should return a Runnable. However, I return a package-private subclass of Runnable instead

    interface MyInterface
        default MyRunnable foo() { ... }

This can be used as

        Runnable r = obj.foo();

However, I don't think the method can be overridden by any subtypes of MyInterface outside the package.

Zhong
Reply all
Reply to author
Forward
0 new messages