Last Updated : 02 Aug, 2025
In Java, a Nested Interface is an interface declared inside a class or another interface. In Java, nested interfaces can be declared with the public, protected, package-private (default) or private access specifiers.
The declaration of the nested interface is:
interface i_first{
interface i_second{
// code
}
}
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 of nested interface Java
//Driver Code Starts
import java.util.*;
// Parent Class
//Driver Code Ends
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
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 Java
//Driver Code Starts
import java.util.*;
class Parent {
protected interface Test {
//Driver Code Ends
void show();
}
}
class Child implements Parent.Test {
public void show(){
System.out.println("show method of interface");
}
}
// Driver Class
//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
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 InterfaceAn 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: Working of interface inside another interface Java
//Driver Code Starts
import java.util.*;
// Nested Interface-Interface
//Driver Code Ends
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
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: Interface cannot have non-public member interface Java
//Driver Code Starts
import java.util.*;
interface 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
class Geeks
{
public static void main(String[] args)
{
Parent.Test obj;
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output:
output Example 3: NestedInterface inside the class Java
//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();
}
}
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 InterfacesIn Java, nested interfaces can be used for a variety of purposes, including:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4