Table of contents
1.
Introduction
2.
Syntax of Nested Interface
3.
Examples of Java Nested-Interface
3.1.
Example 1
3.2.
Java
3.3.
Example 2
3.4.
Java
4.
Interface in another Interface
4.1.
Java
5.
Uses of Nested Interfaces
6.
Frequently Asked Questions
6.1.
Can a nested interface be declared as private?
6.2.
Can a class implement a nested interface directly?
6.3.
Can nested interfaces have static methods?
7.
Conclusion
Last Updated: Aug 3, 2024
Easy

Nested Interface in Java

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, an interface is a blueprint of a class that contains only abstract methods. It specifies what a class must do, but not how it does it. Java also allows interfaces to be defined inside other interfaces, which are known as nested interfaces. Nested interfaces provide a way to logically group related interfaces together. 

Nested Interface in Java

In this article, we will discuss the syntax of nested interfaces, look at examples of how they are used, & learn about their benefits.

Syntax of Nested Interface

To define a nested interface, you simply declare an interface inside another interface or class. The syntax is :

public interface OuterInterface {
    // methods of outer interface
    
    public interface InnerInterface {
        // methods of inner interface
    }
}


The inner interface is declared inside the body of the outer interface. To refer to the inner interface outside the outer interface, you need to use the name of the outer interface followed by a dot & then the name of the inner interface, like this:

OuterInterface.InnerInterface


The nested interface can be declared as public, protected, default, or private, just like any other inner class. 

Examples of Java Nested-Interface

Example 1

  • Java

Java

interface Outer {

   void outerMethod();

  

   interface Inner {

       void innerMethod();

   }

}

class Test implements Outer.Inner {

   public void innerMethod() {

       System.out.println("Inner method");

   }

}

class Main {

   public static void main(String[] args) {

       Outer.Inner obj = new Test();

       obj.innerMethod();

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

Inner method


In this example, we have an outer interface `Outer` which contains a nested interface `Inner`. The `Test` class implements the `Inner` interface & provides the implementation for the `innerMethod()`. In the `Main` class, we create an object of the `Test` class using the `Outer.Inner` interface reference & call the `innerMethod()`.

Example 2

  • Java

Java

interface Vehicle {

   void start();

  

   interface Car {

       void drive();

   }

}

class BMW implements Vehicle.Car {

   public void drive() {

       System.out.println("Driving BMW car");

   }   

   public void start() {

       System.out.println("Starting BMW car");

   }

}

class Main {

   public static void main(String[] args) {

       Vehicle.Car car = new BMW();

       car.drive();

      

       BMW bmw = new BMW();

       bmw.start();

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

Driving BMW car
Starting BMW car


In this example, the `Vehicle` interface contains a nested interface `Car`. The `BMW` class implements both the `Vehicle` & `Car` interfaces. We create an object of the `BMW` class using the `Vehicle.Car` interface reference & call the `drive()` method. We also create a `BMW` object directly & call the `start()` method.

Interface in another Interface

In Java, an interface can be declared inside another interface. This is useful when you want to group related interfaces or when you have interfaces that are only used by another interface. 

For example : 

  • Java

Java

interface Animal {

   void sound();

  

   interface Mammal {

       void feed();

   }

  

   interface Bird {

       void fly();

   }

}

interface Human extends Animal.Mammal {

   void speak();

}

class Child implements Human {

   public void speak() {

       System.out.println("Child speaks");

   }   

   public void feed() {

       System.out.println("Child feeds");

   }

}

class Main {

   public static void main(String[] args) {

       Human child = new Child();

       child.speak();

       child.feed();

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

Child speaks
Child feeds


In this example, we have an `Animal` interface that contains two nested interfaces: `Mammal` & `Bird`. The `Human` interface extends the `Animal.Mammal` interface, indicating that humans are mammals. The `Child` class implements the `Human` interface & provides the implementation for the `speak()` & `feed()` methods.

Uses of Nested Interfaces


1. Logical grouping: Nested interfaces allow you to group related interfaces together. When an interface is only used by another interface or class, it makes sense to nest it inside that interface or class. This improves code organization & readability.
 

2. Encapsulation: By nesting interfaces, you can encapsulate them within the scope of the outer interface or class. This provides better control over the visibility & accessibility of the nested interfaces.
 

3. Namespace management: Nested interfaces help in managing the namespace. When you have multiple interfaces with the same name, you can avoid naming conflicts by nesting them inside different outer interfaces or classes.
 

4. Callback mechanisms: Nested interfaces can be used to define callback methods. The outer interface can define methods that accept instances of the nested interface as parameters, allowing for callback-style communication between classes.
 

5. API design: Nested interfaces can be used to design APIs that have a clear & hierarchical structure. By grouping related functionalities under nested interfaces, you can provide a more intuitive & organized API for developers to use.
 

6. Frameworks & libraries: Many Java frameworks & libraries utilize nested interfaces. For example, the Java Collections Framework extensively uses nested interfaces to define contracts for various collection types, such as `Map.Entry`, `Iterator`, & `Comparator`.
 

7. Event handling: Nested interfaces can be used to define event listeners or event handlers. The outer interface can define methods for registering & unregistering listeners, while the nested interface defines the methods that the listeners must implement.

Frequently Asked Questions

Can a nested interface be declared as private?

Yes, a nested interface can be declared as private. When a nested interface is declared as private, it can only be accessed within the outer interface or class where it is defined. This provides a way to hide the nested interface from external code & maintain encapsulation.

Can a class implement a nested interface directly?

No, a class cannot implement a nested interface directly. To implement a nested interface, the class must first implement the outer interface or class that contains the nested interface. The class can then provide the implementation for the methods defined in the nested interface.

Can nested interfaces have static methods?

Yes, starting from Java 8, interfaces can have static methods. This applies to nested interfaces as well. Static methods in nested interfaces can be accessed using the outer interface name followed by the nested interface name & the method name, like this: OuterInterface.InnerInterface.staticMethod().

Conclusion

In this article, we have learned about nested interfaces in Java. We discussed its syntax & saw examples of how they can be used. We also learned about the benefits of using nested interfaces, like logical grouping, encapsulation, namespace management, & API design. Lastly, we explained how nested interfaces are used in various scenarios, like callback mechanisms, event handling, & frameworks.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass