Public defender methods and static inner classes in interfaces?

Reinier Zwitserloot reinier at zwitserloot.com
Tue Aug 24 03:25:54 PDT 2010


We've covered this on the mailing list before, perhaps you might want to
look at the archives.

The conclusion of some was that this pattern will be rather common, whether
encouraged or not, and this has in fact been used as an argument to allow
specifying the default implementation directly on an interface method,
because this pattern has an ugly wart in it: The inner class must
necessarily be public, and is thus visible (because all members of an
interface are forced public). Making implementation details visible is bad
API design.

One suggestion I made was that 'private' now be allowed on classes inside
interfaces, which would be backwards compatible (package private and
protected remain illegal, and if no access level keyword is given, it'll be
public, in order to remain backwards compatible. The JVM can already do
this), though this idea does depend on whether or not an extension method
has to be visible from the caller, which I'm guessing it has to be, in which
case it can't be private. I can't recall any response to this idea, however.

 --Reinier Zwitserloot



On Tue, Aug 24, 2010 at 4:09 AM, David Walend <david at walend.net> wrote:

> Some style questions: Will it be good taste or bad to include static
> inner classes inside interfaces to provide the default implementation
> of public defender methods? For example
>
> public interface Outer {
>
>     public extension void method(Object parameter)
>        default Inner.method;
>
>     //more extension methods
> ...
>
>     public static final class Inner {
>
>         private Inner() {}
>
>         public static void method(Outer outer,Object parameter) {
>                //some code
>         }
>         //more implementations of extension methods
> ...
>     }
>
> }
>
> Does using a static inner class this way run counter to the goal of
> keeping code out of interfaces (mentioned at then end of section 2 in
> the defender method spec)?
>
> What (if anything) is broken if almost all of the methods declared in
> Outer are implemented by Inner?
>
> Thanks,
>
> Dave
>
>
>


More information about the lambda-dev mailing list