Nested Interface in Java


When an interface as a static member is declared inside a class or another interface, it is called nested interface or member interface. Nested interface must be declared as public inside another interface. The nested interface can be declared in the following general form.
1. Interface inside another interface:
Syntax: 
   public interface Outer {
      // Body of interface.
      public interface inner {
          // Body of interface.
      }
 }
In the preceding interface, the interface Inner is defined as public inside another interface Outer. So it is known as nested interface. Now a class can implement nested interface Inner by using following below statement. 
class Hello implements Outer.Inner {
     // Body of class.
 }
In above example, Outer represents a top-level interface in which interface Inner is a member. Class Hello implements nested interface by specifying "implements Outer.Inner".
For example:
    interface Map {
        interface Entry {
           . . . . . .
       }
  }
Interface Entry is defined inside interface Map. Thus, Entry is nested interface of Map and it can be accessed by calling Map.Entry. A map is a group of key-value pairs (also known as entries). 
2. Interface inside a class: 
Syntax:
     class Outer {
        // Body of class.
         public interface Inner {
            // Body of interface.
     }
}
Now a class can implement member interface Inner by using following statement:
   Class A implements Outer.Inner {
       // Body of class.
   }
In above example, Outer represents a class in which interface Inner is a member.  

Points to Remember for Java Nested Interface


There are some important points regarding in nested interface which have to keep in mind you. They are as follows:
1. Every inner interface or class is always implicitly public and static whether we are declaring or not. 
2. If you declare inner interface inside a class then it can have any access modifiers.
Nested interface in java
3. When you implement outer interface, you are not required to provide implementation for inner interface. We can directly implement the outer interface.
4. Similarly, when you implement an inner interface, you are not required to provide implementation for outer interface because inner interface is always public and static. Hence, we can implement inner interface directly without implementing outer interface.

Java Nested Interface Example Programs


Program source code 1: Let's create a program where we will declare an interface inside another interface and will learn how to access outer and nested interface.
    package nestedInterfaceProgram; public interface Outer { void m1(); // Outer interface contains m1() method. interface Inner { void m2(); // Inner interface contains m2() method. } } // Implementation of top-level interface: public class MyClass1 implements Outer { public void m1(){ System.out.println("Hello"); } } // Implementation of inner interface: public class MyClass2 implements Outer.Inner { public void m2(){ System.out.println("Java"); } } public class Test { public static void main(String[] args) { MyClass1 c1=new MyClass1(); // Creating object of class MyClass1. c1.m1(); // Calling m1() method. MyClass2 c2=new MyClass2(); // Creating object of class MyClass2. c2.m2(); // Calling m2() method. } }
    Output: Hello Java
As you can see output of above program that we can implement independently both Outer and Inner interfaces.

Internal code Generated by the java compiler for nested interface


The java compiler internally adds public and static with nested interface as shown in below coding:
    public static interface Outer$Inner { public abstract void m2(); }

Interface inside Class Example Program


If you require multiple implementations of an interface and all these implementations are related to a particular class, It is highly recommended to define an interface within a class. Let's understand it by an example program.


Program source code 2:
    package nestedInterfaceProgram; public class VehicleTypes { interface Vehicle { public int getNoOfWheels(); } } public class Bus implements VehicleTypes.Vehicle{ public int getNoOfWheels(){ return 6; } } public class Car implements VehicleTypes.Vehicle{ public int getNoOfWheels(){ return 4; } } public class Bike implements VehicleTypes.Vehicle{ public int getNoOfWheels(){ return 2; } } public class VehicleTest { public static void main(String[] args) { Bus b=new Bus(); System.out.println(b.getNoOfWheels()); Car c=new Car(); System.out.println(c.getNoOfWheels()); Bike bk=new Bike(); System.out.println(bk.getNoOfWheels()); } }
    Output: 6 4 2
As you can see in above program, within a class, an interface has multiple implementations provided by multiple classes and all these implementations are related to a particular class. The access modifier for inner interface in the preceding program is default. You can also assign public, protected or private. Let's create another program where we declare protected for nested interface and see what happens?
Program source code 3:
    package nestedInterfaceProgram; public class Cube { protected interface Number { public void calculateCube(int n); } } public class Five implements Cube.Number { public void calculateCube(int n){ int cubeN=n*n*n; System.out.println("Cube of 5: " +cubeN); } } public class Ten implements Cube.Number { public void calculateCube(int n){ int cubeN=n*n*n; System.out.println("Cube of 10: " +cubeN); } } public class CubeTest { public static void main(String[] args) { Five f=new Five(); f.calculateCube(5); Ten t=new Ten(); t.calculateCube(10); } }
    Output: Cube of 5: 125 Cube of 10: 1000
In this particular example program, if we change access modifier to private, we get compiler time error because a derived class tries to access it.

Can we declare Class inside Interface?


If the functionality of class is closely associated with interface functionality, it is highly recommended to declare a class inside interface. In this case, the class will act as like inner class to interface. Let's understand it with the best example program.
Program source code 4:
    interface YahooMail { public void sendMail(EmailDetails ed); class EmailDetails { String to; String cc; String bcc; String subject; String body; . . . } }
In the preceding example program, EmailDetails is required only for YahooMail and we can not use it anywhere else i.e, the functionality of class EmailDetails is associated with an interface YahooMail. Hence, we have declared EmailDetails class inside YahooMail interface.
 
Final words
Hope this tutorial has covered almost all important points related to java nested interface with practical example programs. I hope that you will have understood the basic concept and enjoyed this tutorial.
Thanks for reading!