Open In App

Nested Interface in Java

Last Updated : 10 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.
  • 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.

Syntax:

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 it’s 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: 


// Parent Class
class Parent {
  
  	// Nested Interface
    interface Test {
        void show();
    }
}

// Child Class
class Child implements Parent.Test {
    public void show()


Output
show method of interface

The access specifier in the above example is the default. We can assign public, protected, or private also. 

Example 2: Below is an example of protected. In this particular example, if we change the access specifier to private, we get a compiler error because a derived class tries to access it.


    protected interface Test {
        void show();
    }
}

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



Output
show method of interface

Interface in 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:


// Nested Interface-Interface
interface Parent {
    interface Test {
        void show();
    }
}

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


Output
show method of interface

Note: In the above example, the access specifier is public even if we have not written public. If we try to change the access specifier of an interface to anything other than public, we get a compiler error. Remember, interface members can only be public.

Example 2:


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

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

Error:

illegal combination of modifiers: public and protected
protected interface Yes

Example 3:


    // 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!

In this example, we have a nested interface NestedInterface inside the OuterClass. 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:

  • To group related interfaces together: By nesting one interface within another, you can organize related interfaces in a more logical and readable way. This can make your code easier to understand and maintain.
  • To create more secure code: By making an interface nested inside a class, you can limit its scope and prevent it from being accessed outside of that class. This can make your code more secure and less prone to errors.
  • To implement multiple interfaces: By nesting interfaces, you can implement multiple interfaces in a single class, without cluttering up the global namespace with too many interface names.
  • To create callbacks: Nested interfaces can be used to create callback functions, where an object can be passed to another object and that object can call back a method defined in the nested interface.
  • To define a contract between classes: By using nested interfaces, you can define a contract between classes, where each class implements the same interface, but provides its own implementation. This can make your code more modular and easier to test.


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg