Default interface methods aren't considered abstract method implementations
Pavel Rappo
pavel.rappo at gmail.com
Sun Feb 9 22:26:30 UTC 2025
Attila,
Come to think of it, we should use some other, more narrowly focused,
mailing list. Apologies to subscribers of this list. Since lambda-dev
is defunct now, perhaps we could use compiler-dev, amber-dev, or
core-libs-dev instead. I'm voting for amber-dev, where Brian mostly
resides these days.
-Pavel
On Sun, Feb 9, 2025 at 10:12 PM Pavel Rappo <pavel.rappo at gmail.com> wrote:
>
> You are not specifically asking Brian, are you? If so, sorry for replying.
>
> My humble take is that the default method is something to use when
> there's nothing else there. When there's something, even if it's
> abstract, we use that. So, a default method is a minimally interfering
> fallback/extension scenario, if you will.
>
> That abstract A.m() effectively replaces default F.m() in C as if
> F.m() were never there. If F.m() were allowed to provide
> implementation for A.m() in C, then some uncompliable code could
> suddenly become compilable and operational at runtime.
>
> -Pavel.
>
> On Sun, Feb 9, 2025 at 9:04 PM Attila Szegedi <szegedia at gmail.com> wrote:
> >
> > Hey folks,
> >
> > I found a somewhat puzzling behavior: default interface methods aren't considered abstract method implementations. Trying to compile this minimal reproducer:
> >
> > ------ C.java ------
> > abstract class A {
> > abstract void m();
> > }
> >
> > interface F {
> > default void m() {}
> > }
> >
> > class C extends A implements F {}
> > ------ C.java ------
> >
> > $ javac C.java
> > C.java:9: error: C is not abstract and does not override abstract method m() in A
> > class C extends A implements F {
> > ^
> > 1 error
> >
> > I can accept this being valid according to JLS today (I tried with 11, 21, and 23.) I admit this is one of rare occasions when I didn't go scouring the JLS to figure out what exactly prevents the compiler from accepting F.m() as implementation of A.m() in C. I'm wondering though if this isn't something that could be improved upon in the future. (I'm sure there's a gotcha somewhere. Right, Brian? ;-) )
> >
> > Have a great day,
> > Attila.
More information about the jdk-dev
mailing list