DEV Community

Cover image for Nested Interface in Java: Complete Guide with Examples
Rachit Joshi
Rachit Joshi

Posted on

Nested Interface in Java: Complete Guide with Examples

Java allows an interface to be declared inside another class or interface. Such an interface is called a nested interface. It is useful for grouping related functionality and improving code organization.

A nested interface can be declared inside:

A class
An interface

It helps keep closely related interfaces together and provides better encapsulation.

What Is a Nested Interface?

A nested interface is an interface declared within another class or interface.

Basic Syntax
class OuterClass {

interface InnerInterface {
    void display();
}
Enter fullscreen mode Exit fullscreen mode

}

In this example, InnerInterface is declared inside OuterClass.

Example of a Nested Interface
class Vehicle {

interface Engine {
    void start();
}
Enter fullscreen mode Exit fullscreen mode

}

class Car implements Vehicle.Engine {

public void start() {
    System.out.println("Car engine starts");
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Output
Car engine starts

Here, the Car class implements the nested interface using the syntax Vehicle.Engine.

Nested Interface Inside Another Interface

An interface can also contain another interface.

interface Animal {

void sound();

interface Pet {
    void play();
}
Enter fullscreen mode Exit fullscreen mode

}

class Dog implements Animal, Animal.Pet {

public void sound() {
    System.out.println("Dog makes a sound");
}

public void play() {
    System.out.println("Dog plays with its owner");
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();

    dog.sound();
    dog.play();
}
Enter fullscreen mode Exit fullscreen mode

}

A nested interface declared inside another interface is implicitly public and static.

Advantages of Nested Interfaces

Nested interfaces provide several benefits:

Better organization: Related interfaces can be grouped.

Improved readability: The relationship between classes and interfaces becomes clearer.

Encapsulation: Implementation details can remain within the enclosing class.

Namespace management: Nested interfaces help avoid naming conflicts.

Logical grouping: They are useful when an interface is meaningful only in the context of another class or interface.

Important Points

A nested interface can be declared inside a class or another interface.
It can be implemented using the enclosing class name.

Interfaces declared inside interfaces are implicitly public and static.
A nested interface helps organize related functionality.

It can contain abstract methods, default methods, and static methods according to Java interface rules.

Conclusion

A nested interface in Java is a useful feature for grouping related contracts and maintaining clean code structure. It improves readability, supports encapsulation, and makes large applications easier to organize

Top comments (0)