Skip to content
Advertisement

Selecting default implementation from indirectly inherited Interface not working

I have 4 Classes that look like this:

JavaScript

Why can’t I access the default implementation coming from extending Bar?

Advertisement

Answer

BarImpl can not invoke Foo’s default method explicitly, as BarImpl is not directly implementing Foo. It’s extending Bar which directly implements Foo, hence, it’s Bar’s decision to override Foo’s default method or not.

BarImpl can only invoke Bar’s isEmpty() method via super.isEmpty(), which may end up at Foo’s default method if Bar decides not to override it or at a concrete method of Bar if it does override it.

Note that T.super.method() can only be used if either, T is a directly implemented super interface (i.e. not already implemented by a super class or another super interface) or if T is an enclosing type of an inner class. The second use case is the reason for the “No enclosing instance of … is accessible in scope” error message.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement