Skip to content
Related Articles
Open in App
Not now

Related Articles

Nested Interface in Java

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 19 Feb, 2023
Improve Article
Save Article
Like Article

We can declare interfaces as member of a class or another interface. Such an interface is called as 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 this 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. 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();
    }
}


show method of interface 

The access specifier in above example is default. We can assign public, protected or private also. Below is an example of protected. In this particular example, if we change access specifier to private, we get 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");
    }
}
 
class A
{
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj=t;
        obj.show();
    }
}


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. 

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();
   }
}


show method of interface 

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

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

This article is contributed by Twinkle Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

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.

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.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!