Examples of Java Nested-Interface
Example 1
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
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
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 Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.