Nested Interface in Java | Example Program

When an interface as a static member is declared inside a class or another interface, it is called nested interface in Java or member interface.

Nested interface must be declared as public inside another interface. The nested interface can be declared in the following general form.

1. Interface inside Another Interface

The general syntax to define an interface inside another interface in Java is as follows:

public interface Outer
{
 // Body of interface.
    public interface inner
    {
       // Body of interface.
    }
}

In the preceding syntax, the interface Inner is defined as public inside another interface Outer. So it is known as nested interface.

Now a class can implement nested interface Inner by using the following below statement.

class Hello implements Outer.Inner
 {
     // Body of class
 }

In above example, Outer represents a top-level interface in which interface Inner is a member. Class Hello implements nested interface by specifying “implements Outer.Inner”.

Let’s take another example of declaring inner interface inside outer interface.

interface Map
{
 interface Entry 
 {
   . . . . . .
  }
}

Interface Entry is defined inside the interface Map. Thus, Entry is a nested interface of Map and it can be accessed by calling Map.Entry. A map is a group of key-value pairs (also known as entries).

2. Interface inside Class

The general syntax to define an interface inside a class in Java is as follows:

class Outer
{
// Body of class.
   public interface Inner
   {
     // Body of interface.
   }
}

Now a class can implement member interface Inner by using the following statement:

Class A implements Outer.Inner
{
  // Body of class.
}

In this example, Outer represents a class in which interface Inner is a member.

Points to Remember for Java Nested Interface


There are some important points regarding in nested interface which have to keep in mind you. They are as follows:

1. Every inner interface or class is always implicitly public and static, whether we are declaring or not.

2. If you declare inner interface inside a class, then it can have any access modifiers.
Nested interface in Java
3. When you implement an outer interface, you are not required to provide implementation for inner interface. We can directly implement the outer interface.

4. Similarly, when you implement an inner interface, you are not required to provide implementation for outer interface because inner interface is always public and static. Hence, we can implement an inner interface directly without implementing outer interface.

Java Nested Interface Example Program


Let’s take an example program where we will declare an interface inside another interface and will learn how to access outer and nested interface.

Program code 1:

package nestedInterfaceProgram; 
public interface Outer 
{ 
  void m1(); // Outer interface contains m1() method. 

  interface Inner 
  { 
     void m2(); // Inner interface contains m2() method. 
  } 
} 
// Implementation of top-level interface: 
   public class MyClass1 implements Outer 
   { 
     public void m1()
     { 
       System.out.println("Hello"); 
     } 
   } 
// Implementation of inner interface: 
   public class MyClass2 implements Outer.Inner 
   { 
     public void m2()
     { 
        System.out.println("Java"); 
     } 
   } 
public class Test { 
public static void main(String[] args) 
{ 
 // Creating an object of class MyClass1.
    MyClass1 c1 = new MyClass1();  
     c1.m1(); // Calling m1() method.
 
 // Creating an object of class MyClass2.    
    MyClass2 c2 = new MyClass2();  
     c2.m2(); // Calling m2() method. 
  } 
}
Output: 
       Hello Java

As you can see output of the above program that we can implement independently both Outer and Inner interfaces.

Internal code Generated by the Java compiler for Nested Interface


The Java compiler internally adds public and static with nested interface, as shown in below coding:

public static interface Outer$Inner 
{ 
   public abstract void m2(); 
}

Interface inside Class Example Program


If you require multiple implementations of an interface and all these implementations are related to a particular class, it is highly recommended to define an interface within a class. Let’s understand it through an example program.

Program code 2:

package nestedInterfaceProgram; 
public class VehicleTypes 
{ 
  interface Vehicle 
  { 
    public int getNoOfWheels(); 
  } 
} 
public class Bus implements VehicleTypes.Vehicle 
{ 
  public int getNoOfWheels() 
  { 
    return 6; 
  } 
} 
public class Car implements VehicleTypes.Vehicle 
{ 
  public int getNoOfWheels() 
  { 
    return 4; 
  } 
} 
public class Bike implements VehicleTypes.Vehicle
{ 
  public int getNoOfWheels()
  { 
     return 2; 
   } 
 } 
public class VehicleTest 
{ 
  public static void main(String[] args) 
  { 
    Bus b = new Bus(); 
    System.out.println(b.getNoOfWheels()); 

    Car c = new Car(); 
    System.out.println(c.getNoOfWheels()); 

    Bike bk = new Bike(); 
    System.out.println(bk.getNoOfWheels()); 
   } 
}
Output: 
       6 
       4 
       2

As you can see in the above program, within a class, an interface has multiple implementations provided by multiple classes and all these implementations are related to a particular class.

The access modifier for the inner interface in the preceding program is default. You can also assign public, protected, or private.


Let’s take another example program in which we declare protected for nested interface and see what happens?

Program code 3:

package nestedInterfaceProgram; 
public class Cube 
{ 
  protected interface Number 
  { 
    public void calculateCube(int n); 
   } 
} 
public class Five implements Cube.Number 
{ 
  public void calculateCube(int n)
  { 
    int cubeN = n * n * n; 
    System.out.println("Cube of 5: " +cubeN); 
   } 
} 
public class Ten implements Cube.Number 
{ 
  public void calculateCube(int n)
  { 
    int cubeN = n * n * n; 
    System.out.println("Cube of 10: " +cubeN); 
   } 
} 
public class CubeTest 
{ 
  public static void main(String[] args) 
  { 
    Five f = new Five(); 
     f.calculateCube(5); 

    Ten t = new Ten(); 
     t.calculateCube(10); 
  } 
}
Output: 
       Cube of 5: 125 
       Cube of 10: 1000

In this particular example program, if we change the access modifier to private, we get compile-time error because a derived class tries to access it.

Can We declare Class inside Interface?


If the functionality of class is closely associated with interface functionality, it is highly recommended to declare a class inside the interface. In this case, the class will act like an inner class to interface. Let’s understand it with the best example program.

Program code 4:

interface YahooMail 
{ 
 public void sendMail(EmailDetails ed); 
 class EmailDetails 
 { 
   String to; 
   String cc; 
   String bcc; 
   String subject; 
   String body; 
   . 
   . 
   . 
 } 
}

In the preceding example program, EmailDetails is required only for YahooMail and we can not use it anywhere else i.e, the functionality of class EmailDetails is associated with an interface YahooMail. Hence, we have declared EmailDetails class inside YahooMail interface.


FAQs on Nested Interface

1. What is a nested interface in Java?

2. Can we have an interface inside interface in Java?

3. Can a class have an interface?


In this tutorial, we have explained almost all important points related to Java nested interface through example programs. Hope that you will have understood the basic concept and enjoyed this tutorial. In the next, we will understand about the difference between abstract class and interface in Java with the help of example.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love