Java Nested InterfaceAn interface, i.e., declared within another interface or class, is known as a 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 to by the outer interface or class. It can't be accessed directly. Points to remember for nested interfacesThere are given some points that should be remembered by the java programmer.
Syntax of nested interface which is declared within the interfaceSyntax of nested interface which is declared within the classExample of nested interface which is declared within the interfaceIn this example, we will learn how to declare the nested interface and how we can access it. TestNestedInterface1.java Test it NowOutput: hello nested interface As you can see in the above example, we are accessing the Message interface by its outer interface Showable because it cannot be accessed directly. It is just like the almirah inside the room; we cannot access the almirah directly because we must enter the room first. In the collection framework, the 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 MessageThe java compiler internally creates a public and static interface as displayed below: Example of nested interface which is declared within the classLet's see how we can define an interface inside the class and how we can access it. TestNestedInterface2.java Test it NowOutput: hello nested interface Can we define a class inside the interface?Yes, if we define a class inside the interface, the Java compiler creates a static nested class. Let's see how can we define a class within the interface: Next TopicJava Multithreading |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India