Open In App

Nested Interface in Java

Last Updated : 22 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interface in a class Interfaces (or classes) can have only public and default access specifiers when declared outside any other class. Refer to Access modifiers for classes or interfaces in the Java article for details. This interface declared in a class can either be default, public, protected not private. While implementing the interface, we mention the interface as c_name.i_name where c_name is the name of the class in which it is nested and i_name is the name of the interface itself. 

Syntax of Nested Interface

interface first{  
     interface second{  
          ...      
     }  
}

There is another nested interface which is nested inside a class it’s syntax is as follows:

class abc{
    interface _interface_name{
        ...
    }
}

Examples of Java Nested-Interface

The following are examples that demonstrate the use of nested interfaces in different situations.

Example 1

Let us have a look at the following code: 

Java




// Java program to demonstrate working of
// interface inside a class.
import java.util.*;
class Test {
    interface Yes {
        void show();
    }
}
 
class Testing implements Test.Yes {
    public void show()
    {
        System.out.println("show method of interface");
    }
}
 
class A {
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj = t;
        obj.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.

Java




// Java program to demonstrate protected
// specifier for nested interface.
import java.util.*;
 
class Test {
    protected interface Yes {
        void show();
    }
}
 
class Testing implements Test.Yes {
    public void show()
    {
        System.out.println("show method of interface");
    }
}
 
// Driver Class
class A {
    // main function
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj = t;
        obj.show();
    }
}


Output

show method of interface

Interface in another Interface

An interface can be declared inside another interface also. We mention the interface as i_name1.i_name2 where i_name1 is the name of the interface in which it is nested and i_name2 is the name of the interface to be implemented

Example 1

Java




// Java program to demonstrate working of
// interface inside another interface.
import java.util.*;
interface Test {
    interface Yes {
        void show();
    }
}
 
class Testing implements Test.Yes {
    public void show()
    {
        System.out.println("show method of interface");
    }
}
 
class A {
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj = t;
        obj.show();
    }
}


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

Java




// Java program to demonstrate an interface cannot
// have non-public member interface.
import java.util.*;
interface Test {
    protected interface Yes {
        void show();
    }
}
 
class Testing implements Test.Yes {
    public void show()
    {
        System.out.println("show method of interface");
    }
}
 
class A {
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj = t;
        obj.show();
    }
}


illegal combination of modifiers: public and protected
   protected interface Yes

Here’s another example Java program that demonstrates the use of nested interfaces:

Java




public class OuterClass {
 
    // 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads