Interface inside Interface

Note: It is advised to read the basics and types of inner classes before proceeding further.

A nested interface is just an interface but declared within another interface or even a class.

A top-level interface is that one which is not nested.

Nested interfaces are useful to group all the interfaces with some common functionality. It is to group the interfaces having some common functionality (purpose). Following two codes illustrates.

interface Structure
{
  interface constructPillars
  {
    // some abstract methods used for constructing pillars
  }
  interface constructCeiling
  {
    // some abstract methods used for constructing ceiling
  }
  interface constructWalls
  {
    // some abstract methods used for constructing walls
  }
}

To construct a structure, three interfaces are giving the way of constructing pillars, ceiling and walls. Interfaces Pillars, Ceiling and Walls are having a common functionality.

One more practical approach can be seen as in the following code.

public interface Structure
{
  public interface CommercialBuilding
  {
    int numberOfPillars = 12;  
    int pillarSpan = 20; 
  }
}

The usage here, you can observe, is no other application other than Structure can modify the number and span of pillars to construct the CommercialBuilding, even accidentally.

interface Outer1              
{
  interface Inner1      
  {                                
    public abstract void display(); 
  }
}

public class Test implements Outer1.Inner1
{
  public void display()
  {
    System.out.println("Hello 1");
  }
  public static void main(String args[])
  {
    Test t1 = new Test();
    t1.display();                
  }
}

Let us go little bit deeper.

Following code also compiles and executes.

interface Outer1              
{
  public abstract void show(); 
  interface Inner1      
  {                                
    public abstract void display(); 
  }
}

public class Test implements Outer1.Inner1
{
  public void display()
  {
    System.out.println("Hello 1");
  }
  public static void main(String args[])
  {
    Test t1 = new Test();
    t1.display();                
  }
}

In the above code show() is not overridden as Outer1 is not implemented. It is done in the next program.

interface Outer1              
{
  public abstract void show(); 
  interface Inner1      
  {                                
    public abstract void display(); 
  }
}

public class Test implements Outer1, Outer1.Inner1
{
  public void display()
  {
    System.out.println("Hello 1");
  }
  public void show()
  {
    System.out.println("Hello 2");
  }
  public static void main(String args[])
  {
    Test t1 = new Test();
    t1.display();                
    t1.show();                
  }
}

That is, the Test class can implement Outer1 and Inner1 separately or both together.

That is, following statement also executes.

    public class Test implements Outer1

Note: The implementing class can implement either the top-level interface alone or only nested interface or both.

Leave a Comment

Your email address will not be published.