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 :
interfaceOuterInterfaceName {interfaceInnerInterfaceName {// constant declarations// Method declarations} } Example :interfaceMyInterface {interfaceMyInnerInterface {intid = 20;voidprint(); } }
Nested interface can be defined anywhere inside outer interface or class. Some of the key points about nested interfaces are :
- Nested interfaces are 
staticby default, irrespective of you declare itstaticor 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 
publicby 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
interfaceMyInterface {voidcalculateArea();interfaceMyInnerInterface {intid = 20;voidprint(); } }classNestedInterfaceimplementsMyInterface.MyInnerInterface {public voidprint() { System.out.println("Print method of nested interface"); }public static voidmain(String args []) { NestedInterface obj =newNestedInterface(); 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
classOuterClass {interfaceMyInnerInterface {intid = 20;voidprint(); } }classNestedInterfaceDemoimplementsOuterClass.MyInnerInterface {public voidprint() { System.out.println("Print method of nested interface"); }public static voidmain(String args []) { NestedInterfaceDemo obj =newNestedInterfaceDemo(); obj.print(); System.out.println(obj.id);// Assigning the object into nested interface typeOuterClass.MyInnerInterface obj2 =newNestedInterfaceDemo(); 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.
interfaceMyInterface {voidcalculateArea();interfaceMyInnerInterface {intid = 20;voidprint(); } }classOuterInterfaceTestimplementsMyInterface {public voidcalculateArea() { System.out.println("Calculate area inside this method"); }public static voidmain(String args []) { OuterInterfaceTest obj =newOuterInterfaceTest(); 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.
 


