Nested Interfaces in Java

Similar to nested classes we have nested interfaces in java. An interface defined inside another interface or class is known as nested interface. Nested interface is also known as inner interfaces or member interfaces. Nested interfaces are generally used to group related interfaces. The syntax of declaring nested interface is :

 interface OuterInterfaceName { 
 
   interface InnerInterfaceName {
       // constant declarations   
       // Method declarations
    }
 }    
  
 Example :  
 
 interface MyInterface {
 
   interface MyInnerInterface {
      int id = 20;
      void print();
    }
 }        

Nested interface can be defined anywhere inside outer interface or class. Some of the key points about nested interfaces are :

  • Nested interfaces are static by default, irrespective of you declare it static or not.
  • Nested interfaces are accessed using outer interface or class name.
  • Nested interfaces declared inside a class can have any access modifier, while nested interfaces declared inside another interfaces are public by default.
  • Classes implementing inner interface are required to implement only inner interface methods, not outer interface methods.
  • Classes implementing outer interface are required to implement only outer interface methods, not inner interface methods.

Does java compiler creates separate .class file for nested interfaces ?

Yes, Java compiler creates separate .class file for nested interfaces, whose name is made with combination of outer and inner interface name with $ sign in between. For example the name of .class file in above declaration would be MyInterface$MyInnerInterface.class.

Nested interface program in Java

 interface MyInterface {
    void calculateArea();
    interface MyInnerInterface {
       int id = 20;
       void print();     
    }
  }
  
 class NestedInterface implements MyInterface.MyInnerInterface {   
    public void print() {
       System.out.println("Print method of nested interface");
     }    
    public static void main(String args []) {
       NestedInterface obj = new NestedInterface();
       obj.print();
       System.out.println(obj.id);
    }
 }           

Output:

Print method of nested interface
20

As mentioned above nested interface can be declared inside a class as well. Class implementing such interface needs to define all the methods of that nested interface. The program below demonstrates the nested interface declared inside a class.

Java program of nested interface declared inside a class

 class OuterClass { 
    interface MyInnerInterface {
       int id = 20;
       void print();     
    }
  }
 
 class NestedInterfaceDemo implements OuterClass.MyInnerInterface {   
    public void print() {
       System.out.println("Print method of nested interface");
     }    
    public static void main(String args []) {
       NestedInterfaceDemo obj = new NestedInterfaceDemo();       
       obj.print();
       System.out.println(obj.id);
       // Assigning the object into nested interface type
       OuterClass.MyInnerInterface obj2 = new NestedInterfaceDemo();
       obj2.print();
    }
 }           

Output:

Print method of nested interface
20
Print method of nested interface

The object of implementing class can also be assigned in to nested interface type, as in above program obj2 is an object of NestedInterfaceDemo class but assigned into MyInnerInterface type.

As mentioned above class implementing an outer interface are not required to implement the inner or nested interface methods. The program below demonstrates the same.

 interface MyInterface {
    void calculateArea();
    interface MyInnerInterface {
       int id = 20;
       void print();     
     }
  }
 
 class OuterInterfaceTest implements MyInterface {   
    public void calculateArea() {
       System.out.println("Calculate area inside this method");
     }    
    public static void main(String args []) {
       OuterInterfaceTest obj = new OuterInterfaceTest();
       obj.calculateArea();
     }
 }           

Output:

Calculate area inside this method

Why do we use nested interface in java

There are couple of reasons for using nested interfaces in java :

  • Nesting of interfaces is a way of logically grouping interfaces which are related or used at one place only.
  • Nesting of interfaces helps to write more readable and maintainable code.
  • It also increases encapsulation.

One example of nested interface in java library is java.util.Map.Entry interface defined inside java.util.Map interface. We access it as Map.Entry, as it's a nested interface.

★★★
  • An interface or class can have any number of inner interfaces inside it.
  • Inner interface can extend it's outer interface.
  • Inner and outer interfaces can have method and variables with same name.
  • Nested interfaces can also have default and static methods from java 8 onward.
  • An inner class defined inside an interface can implement the interface.
  • Nesting of interfaces can be done any number of times. As a good practice you should avoid doing nesting more than once.