Java Nested Interface

An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface. Since nested interface cannot be accessed directly, the main purpose of using them is to resolve the namespace by grouping related interfaces (or related interface and class) together. This way, we can only call the nested interface by using outer class or outer interface name followed by dot( . ), followed by the interface name.

An interface i.e. declared within another interface or class is known as nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred by the outer interface or class. It can't be accessed directly.

Points to remember for nested interfaces

There are given some points that should be remembered by the java programmer.

  • Nested interface must be public if it is declared inside the interface but it can have any access modifier if declared within the class.

  • Nested interfaces are declared static implicitely.

Syntax of nested interface which is declared within the interface
interface interface_name { ... interface nested_interface_name { ... } }
Syntax of nested interface which is declared within the class
class class_name{ ... interface nested_interface_name{ ... } }

Example: Entry interface inside Map interface is nested. Thus we access it by calling Map.Entry.

Note:
  • Nested interfaces are static by default. You don’t have to mark them static explicitly as it would be redundant.

  • Nested interfaces declared inside class can take any access modifier, however nested interface declared inside interface is public implicitly.

Example : Nested interface declared inside another interface
interface MyInterfaceA { void display(); interface MyInterfaceB { void myMethod(); } } class NestedInterfaceDemo1 implements MyInterfaceA.MyInterfaceB { public void myMethod() { System.out.println("Nested interface method"); } public static void main(String args[]){ MyInterfaceA.MyInterfaceB obj= new NestedInterfaceDemo1(); obj.myMethod(); } }


Output:
Nested interface method
Example : Nested interface declared inside a class
class MyClass { interface MyInterfaceB { void myMethod(); } } class NestedInterfaceDemo2 implements MyClass.MyInterfaceB { public void myMethod() { System.out.println("Nested interface method"); } public static void main(String args[]){ MyClass.MyInterfaceB obj= new NestedInterfaceDemo2(); obj.myMethod(); } }


Output:
Nested interface method
Example of nested interface which is declared within the interface
interface Showable { void show(); interface Message { void msg(); } } class Test implements Showable.Message { public void msg() { System.out.println("Hello nested interface in Java"); } public static void main(String args[]) { Showable.Message message=new Test();//upcasting here message.msg(); } }


Output:
Hello nested interface in Java

As you can see in the above example, we are acessing the Message interface by its outer interface Showable because it cannot be accessed directly. It is just like almirah inside the room, we cannot access the almirah directly because we must enter the room first. In collection frameword, sun microsystem has provided a nested interface Entry. Entry is the subinterface of Map i.e. accessed by Map.Entry.

Internal code generated by the java compiler for nested interface Message

The java compiler internally creates public and static interface as displayed below:.

public static interface Showable$Message { public abstract void msg(); }
Example of nested interface which is declared within the class
class A { interface Message { void msg(); } } class Test implements A.Message { public void msg(){System.out.println("Hello nested interface in java");} public static void main(String args[]){ A.Message message=new Test();//upcasting here message.msg(); } }


Output:
Hello nested interface in java
Can we define a class inside the interface?

Yes, If we define a class inside the interface, java compiler creates a static nested class. Let's see how can we define a class within the interface:

interface M { class A{} }



Instagram