Nested Interface in Java
In Java, we can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interfaces declared outside any class can have only public and default (package-private) access specifiers. In Java, nested interfaces (interfaces declared inside a class or another interface) can be declared with the public, protected, package-private (default), or private access specifiers.
- A nested interface can be declared public, protected, package-private (default), or private. But if we put an interface inside another interface, it is automatically public and static, it simply means that we do not need to add public or static ourselves.
- A top-level interface (not nested) can only be declared as public or package-private (default). It cannot be declared as protected or private.
Refer to the article: Access Modifiers for Classes or Interfaces in Java for more details.
Declaration of Nested Interface
The declaration of the nested interface is:
interface i_first{
interface i_second{
...
}
}
When implementing a nested interface, we refer to it as i_first.i_second, where i_first is the name of the interface in which the interface is nested, and i_second is the interface's name.
There is another nested interface which is nested inside a class its syntax is as follows:
class c_name{
interface i_name{
...
}
}
When implementing a nested interface, we refer to it as c_name.i_name, where c_name is the name of the class in which the interface is nested and i_name is the interface's name.
Example 1: Let us have a look at the following code:
//Driver Code Starts
// Working of interface inside a class
import java.util.*;
//Driver Code Ends
// Parent Class
class Parent {
// Nested Interface
interface Test {
void show();
}
}
// Child Class
class Child implements Parent.Test {
public void show()
//Driver Code Starts
{
System.out.println("show method of interface");
}
}
class Geeks
{
public static void main(String[] args)
{
// instance of Parent class
// with Nested Interface
Parent.Test obj;
// Instance of Child class
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output
show method of interface
Explanation: The access specifier of the nested interface Test is package-private (default) since no access modifier is specified. We can also assign public, protected, or private access specifiers to nested interfaces inside a class.
Example 2: Below is an example of protected Nested Interface.
//Driver Code Starts
// Protected specifier for nested interface
import java.util.*;
class Parent {
//Driver Code Ends
protected interface Test {
void show();
}
}
class Child implements Parent.Test {
public void show(){
System.out.println("show method of interface");
}
}
//Driver Code Starts
// Driver Class
class Geeks
{
public static void main(String[] args)
{
Parent.Test obj;
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output
show method of interface
Explanation: In the above example, if we change the access specifier to private, it will cause a compilation error because the derived class Child tries to access a private interface.
Interface Nested Inside Another Interface
An interface can be declared inside another interface also. We mention the interface as Parent.Test where Parent is the name of the interface in which it is nested and Test is the name of the interface to be implemented.
Example 1:
//Driver Code Starts
// Working of interface inside another interface
import java.util.*;
//Driver Code Ends
// Nested Interface-Interface
interface Parent {
interface Test {
void show();
}
}
class Child implements Parent.Test {
public void show() {
System.out.println("show method of interface");
}
}
//Driver Code Starts
// Main Class
class Geeks
{
public static void main(String[] args)
{
Parent.Test obj;
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output
show method of interface
Explanation: In the above example, when we put an interface inside another interface it is automatically public and static even if we do not write public and if we try to make it private and protected, the compiler will give an error. Everything inside an interface is always considered public by default.
Example 2:
//Driver Code Starts
// Interface cannot have non-public member interface
import java.util.*;
//Driver Code Ends
interface Parent {
protected interface Test {
void show();
}
}
class Child implements Parent.Test {
public void show()
{
System.out.println("show method of interface");
}
}
//Driver Code Starts
class Geeks
{
public static void main(String[] args)
{
Parent.Test obj;
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output:
Example 3:
//Driver Code Starts
public class Geeks {
//Driver Code Ends
// Nested interface
public interface NestedInterface {
public void nestedMethod();
}
public static void main(String[] args)
{
// Implement nested interface
NestedInterface nested = new NestedInterface() {
public void nestedMethod()
{
System.out.println(
"Hello from nested interface!");
}
};
// Call nested interface method
nested.nestedMethod();
}
}
Output
Hello from nested interface!
Explanation: In this example, we have a nested interface NestedInterface inside the outer class. We then implement the interface using an anonymous inner class in the main method and call its method nestedMethod(). This is just one way to use nested interfaces in Java.
Uses of Nested Interfaces
In Java, nested interfaces can be used for a variety of purposes, including:
- When we put one interface inside another interface it makes the code more organized and easy to understand as well.
- If we nest an interface inside a class, it limits where that interface can be used. This helps keep our code safer and reduces the chances because the interface won’t be accessible everywhere.
- Nested interfaces are great for callbacks. This means one object can pass itself to another, and the second object can call a method defined inside the nested interface.
- By using nested interfaces, we can set up a contract. Different classes can follow this contract by implementing the same interface but with their own versions