Java Static Nested Interfaces

What is Nested Interface?

A nested interface, as mentioned in Java language specification, is any regular interface whose declaration occurs within the body of another class or interface. Nested interface is also called inner interface. A top level interface is an interface that is not a nested interface. Regardless of top level or nested interface, an interface declaration introduces a new hundred percent abstract reference type, whose members are classes, interfaces, constants, and abstract methods. By creating an interface, we define a contract for what a class can do if it implements the interface, without saying anything about how the class will do it. An interface cannot be instantiated on its own; therefore, it must be implemented by a class or extended by other interface to be used.

Rules for Declaring Nested Interface

To declare a nested interface there are certain rules. These rules apply depending upon where the nested interface declaration occurs inside another interface or class.

  • Nested interfaces are implicitly static regardless of where they are declared (inside class or another interface).
  • Nested interface declared inside another interface is implicitly public.
  • Nested interface declared inside a class can accept any access modifiers.
  • Nested interface can be implemented by any class (package level, nested or inner) if the access modifiers permit visibility.

Nested Interface Declaration within Interface

As said earlier, when an interface is declared within another interface it is implicitly public and static; therefore, adding those modifiers is considered redundant and makes no difference. Given that rule, we can see that the following two nested interface declarations are exactly equivalent:

interface A
{
  interface NestedA { void aMethod(); }
  interface NestedAA { void aaMethod(); }
}

Above nested interface declaration for NestedA and NestedAA is equivalent to the following declaration because modifiers public and static are implicit. However, the compiler would not complain if an implicit modifier is mentioned explicitly by programmers but this is not considered as a good practice and you should avoid doing that. And so, if a modifier comes more than one time in a declaration, it would be a compile time error.

interface A
{
  public static interface NestedA { void aMethod(); }
  public static interface NestedAA { void aaMethod(); }
}

The following example demonstrates how nested interfaces are declared within other interfaces and used.

/* NestedInterfaceDemo.java */
interface A
{
  interface NestedA { void aMethod(); }
 
  // modifier public and static are placed for 
  // demonstration, in real life avoid placing modifiers that
  // are implicit to declarations
  public static interface NestedAA { void aaMethod(); }
}
 
public class NestedInterfaceDemo implements A.NestedA, A.NestedAA
{
  public static void main (String args[])
  {
    A.NestedA na = new NestedInterfaceDemo();
    na.aMethod();
    A.NestedAA naa = (A.NestedAA) na;
    naa.aaMethod();
  }
 
  public void aMethod()
  {
    System.out.println("within from aMethod");
  }
 
  public void aaMethod()
  {
    System.out.println("within from aaMethod");
  }
}
 
OUTPUT
======
D:\JavaPrograms>javac NestedInterfaceDemo.java
D:\JavaPrograms>java NestedInterfaceDemo
within from aMethod
within from aaMethod

Nested Interface Declaration within Class

One rule is common for nested interfaces that they are implicitly static; no matter if they are declared within an interface or a class. But visibility of a nested interface can be controlled when it is declared inside a class. As we have seen, inside an interface a nested interface is always public but this is not the case when a nested interface is declared inside a class. You can declare a nested interface public, protected, or private depending upon the level of encapsulation. However, a private nested interface declared within a class can only be extended by other nested interfaces or implemented by other nested or inner classes. It cannot be accessed from outside the class that contains it.

Let's take a look at the following example, and you will understand that how you can control the visibility of a nested interface when it is declared inside a class.

/* NestedInterfaceDemo.java */
class A
{
  private interface NestedPA { void paMethod(); }
  protected interface NestedA extends NestedPA { void aMethod(); }
  public interface NestedAA { void aaMethod(); }
}
 
public class NestedInterfaceDemo implements A.NestedA, A.NestedAA
{
  public static void main (String args[])
  {
    A.NestedA na = new NestedInterfaceDemo();
    na.aMethod();
    na.paMethod();
 
    A.NestedAA naa = (A.NestedAA) na;
    naa.aaMethod();
  }
 
  public void aMethod()
  {
    System.out.println("within from aMethod");
  }
 
  public void aaMethod()
  {
    System.out.println("within from aaMethod");
  }
 
  public void paMethod()
  {
    System.out.println("within from paMethod");
  }
}
 
OUTPUT
======
D:\JavaPrograms>javac NestedInterfaceDemo.java
 
D:\JavaPrograms>java NestedInterfaceDemo
within from aMethod
within from paMethod
within from aaMethod

Application Use of Nested Interfaces

A static nested interface serves a great advantage to namespace resolution. This is the basic idea behind introducing nested interfaces and nested classes in Java. For example, if you have an interface with an exceedingly common name, and in a large project, it is quite possible that some other programmer has the same idea, and has an interface with the same name you had, then you can solve this potential name clash by making your interface a public static nested interface. And your interface will be known as outer class or outer interface, followed by a period (.), and then followed by static nested interface name.

Last Word

In this tutorial we discussed static nested interfaces or inner interfaces in Java. Static nested interfaces in classes are useful and help maintain a clean design. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.