In my opinion, interfaces serve to separate abstraction and implementation. Actually, why might you need to create a class inside the interface that will have some implementation? I would like to see a real example of the use of inner classes in interfaces in order to understand how such a Java feature can be used.

  • one
    I saw this once - inside the interfaces there were internal classes that could create objects that implement these interfaces, and a heap of all utilitarian methods. In my opinion, this is not correct, for the same reason that you indicated. Just, sometimes, the fact that something can be done does not mean that it needs to be done :) - Sergi

1 answer 1

I ran through the structured search in my CLASSPATH and caught the following examples:

  1. Interface as namespace

    1. The most frequent is a static еnum , connected by a domain to an interface, declared inside the interface. The interface in this case plays the role of a namespace.

       public interface Foo { void foo(Bar bar); static enum Bar { BAR1, BAR2, BAR3 } } 

      Example:


    1. A similar variant is the class describing the value-type associated with the interface.

       public interface Foo { void foo(Bar bar); static class Bar { private final String id; public Bar(Sting id) { this.id = id; } public String getId() { return id; } } } 

      Example:


    1. Several small classes inside the interface implement another very narrow interface. The interface is used only as a namespace.

       public interface Convertor { String convert(Object arg); } public interface Convertors { static class Convertor1 implements Convertor { String convert(Object arg) { /* ... */ }} static class Convertor2 implements Convertor { String convert(Object arg) { /* ... */ }} static class Convertor3 implements Convertor { String convert(Object arg) { /* ... */ }} } 

      Example:


    1. The exception associated with the interface.

       public interface Foo { void foo() throws BarException; static class BarException extends Exception { // ... } } 

      Example:


    1. The interface declares the helper helper classes with logic that the implementations suggest to use.

      Example:


    1. The interface declares inside itself a ready-made stub implementation.

       public interface Foo { Bar foo(); static class FooImpl implements Foo { @Override public void foo() { return null; } } } 

      Example:


    1. The interface declares inside itself Singleton, because "why not": it’s already antipattern, so at least localize it.

      Example:


  1. Annotation + handler

    A static class that implements a certain interface within an annotation interface that describes the logic of this annotation. Code analyzing annotations is likely to get to this static class through reflection.

      @Retention(RetentionPolicy.RUNTIME) public @interface Foo { // annotation fields static class FooHandler implements Handler<Foo> { public void handle(Foo foo, Object arg) { //... } } } public interface Handler<A extends Annotation> { void handle(A a, Object arg); } 

    Examples:

    • javax.annotation.RegEx
    • javax.annotation.Nonnull
    • javax.annotation.Nonnegative