Static Nested Interfaces

A nested interface is just a regular interface defined inside another class or interface. They are actually defined inside the body of the parent class, not only in the same file. The feature is useful for grouping related interfaces and for encapsulating interfaces in the classes where they are used.

Nested interfaces facts:

  • when declared inside another interface they can only be public
  • when declared inside classes they can accept any access modifiers
  • they are implicitly static
  • they can be implemented by any class (package level, nested or inner) as long as the access modifiers permit visibility
  • as with regular package level interfaces variables defined inside are considered to be constants (static public) regardless of whether the modifiers are specified
  • like the package level interfaces they can declare nested classes and interfaces
  • While declaring nested interfaces in classes is useful and can help maintain a clean design, other constructs like classes nested in interfaces and interfaces nested in interfaces are of little benefit and seem outright weird and even dangerous.

    The following code snippets show the syntax associated with nested interfaces:

    package com.littletutorials.nested;
    
    interface TopIf
    {
        interface NestedIf1 // implicitly public
        {
            interface NestedIf2
            {
                String fNestedIf2();
            }
    
            String fNestedIf1();
        }
    
        class NestedCls // implicitly public
        {
            public String fNestedCls()
            {
                return "fNestedCls";
            }
        }
    
        String fTopIf(); // implicitly public
    }
    

    The code above shows how to declare nested interfaces and even a class nested in an interface (not very useful).

    Now let’s see how to implement these interfaces in various scenarios:

    package com.littletutorials.nested;
    
    public class ImplTopIf implements TopIf
    {
        public static class NestedImplNestedIf1 implements NestedIf1
        {
            @Override
            public String fNestedIf1()
            {
                return "NestedIf1 implementation";
            }
        }
    
        public static class NestedImplNestedIf2 implements NestedIf1.NestedIf2
        {
            @Override
            public String fNestedIf2()
            {
                return "NestedIf2 implementation";
            }
        }
    
        public interface NestedIf3
        {
            String fNestedIf3();
        }
    
        public static class NestedImplNestedIf3 implements NestedIf3
        {
            @Override
            public String fNestedIf3()
            {
                return "NestedIf3 implementation";
            }
        }
    
        @Override
        public String fTopIf()
        {
            return "fTopIf implementation";
        }
    }
    
    class ImplNestedIf3 implements ImplTopIf.NestedIf3
    {
        @Override
        public String fNestedIf3()
        {
            return "NestedIf3 implementation";
        }
    }
    

    Nesting interfaces in classes presents clear benefits related to encapsulation, for example when inner classes will implement them. Also declaring interfaces inside other interfaces can sometimes greatly improve the elegance of the design. A good example for this is the java.util.Map interface that embeds the declaration of the java.util.Map.Entry interface. The rest of constructs look more like syntactic sugar with the same nutritional benefits to your design. I would like to hear from somebody who used classes nested in interfaces for a very good reason.

    This post is part of a series explaining the Java concept of defining classes in other classes: