Open In App

Nested Interface in Java

Last Updated : 14 Nov, 2025
Comments
Improve
Suggest changes
98 Likes
Like
Report

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.

  • Inside a class, it can be public, protected, default, or private.
  • Inside another interface, it is implicitly public and static.
  • A top-level interface can only be public or default (package-private).

There are two types of nested interfaces in Java

1. Nested Interface Inside a Class

When an interface is declared inside a class, it can have any access modifier. To implement it, you must qualify it with the outer class name.

Java
class Outer {
    interface Inner {
        void display();
    }
}

class GFG implements Outer.Inner{
    
    public void display(){
        
        System.out.println(
            "Hello from Nested Interface Inside a Class!");
    }

    public static void main(String[] args){
        
        Outer.Inner obj = new GFG();
        obj.display();
    }
}

Output
Hello from Nested Interface Inside a Class!

Explanation:

  • The interface Inner is nested inside the class Outer.
  • The class GFG implements Outer.Inner.
  • The method display() is defined and called through the interface reference.

2. Nested Interface Inside an Interface

When an interface is declared inside another interface, it is implicitly public and static, even if not explicitly declared.

Java
interface OuterInterface {

    void show();

    interface InnerInterface {
        void msg();
    }
}

class Test implements OuterInterface.InnerInterface {

    public void msg()
    {

        System.out.println(
            "Message from Nested Interface Inside an Interface!");
    }

    public static void main(String[] args){
        
        OuterInterface.InnerInterface obj = new Test();
        obj.msg();
    }
}

Output
Message from Nested Interface Inside an Interface!

Explanation:

  • The InnerInterface is defined inside OuterInterface.
  • Since nested interfaces inside another interface are public static by default, Test can directly implement it.
  • The fully qualified name OuterInterface.InnerInterface is used to refer to it.

Key Rules of Nested Interfaces

  • Inside a Class: A nested interface can be public, protected, package-private (default), or private.
  • Inside an Interface: It is implicitly public and static, even if not explicitly mentioned.
  • Top-Level Interfaces: A top-level interface (not nested) can only be public or package-private (default), it cannot be protected or private.

Example: Interface cannot have non-public member interface

Java
import java.util.*;

interface Parent{
    
    protected interface Test{
        
        void show();
    }
}

class Child implements Parent.Test{
    
    public void show(){
        
        System.out.println("show method of interface");
    }
}

class Geeks{
    
    public static void main(String[] args){
        
        Parent.Test obj;
        Child t = new Child();
        obj = t;
        obj.show();
    }
}


Output:

Output
output

Uses of Nested Interfaces

In Java, nested interfaces can be used for a variety of purposes, including:

  • Organizing Code: Grouping related interfaces improves structure and readability.
  • Controlled Access: Nesting an interface inside a class limits its visibility, enhancing safety.
  • Callbacks: Enables one object to pass itself to another for method invocation.
  • Defining Contracts: Multiple classes can implement the same interface with their own versions, ensuring consistent behavior.


Suggested Quiz
3 Questions

How is a nested interface defined in Java?


  • A

    An interface declared inside another interface or class

  • B

    An interface that extends another interface

  • C

    An interface that has private methods

  • D

    An interface with only default methods

Explanation:

A nested interface is an interface declared inside another interface or class and is implicitly public and static.


What is a nested interface in Java?


  • A

    Interface inside a method


  • B

    Interface inside a class or interface

  • C

    Interface inside a package


  • D

    Static interface

Explanation:


Which of the following statements about nested interfaces in Java is true?

  • A

    A nested interface inside a class can be public, protected, private, or package-private

  • B

    A nested interface inside another interface is automatically public and static

  • C

    A top-level interface cannot be declared private or protected

  • D

    All of the above

Explanation:


Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Article Tags :

Explore