Nested Interface in Java
In Java, a Nested Interface is an interface declared inside a class or another interface. In Java, nested interfaces can be declared with the public, protected, package-private (default) or private access specifiers.
- A nested interface inside a class can be public, protected, package-private (default) or private.
- A nested interface inside another interface is implicitly public static.
- A top-level interface (not nested) can only be public or package-private (default); it cannot be protected or private.
Declaration of Nested Interface
The declaration of the nested interface is:
interface i_first{
interface i_second{
// code
}
}
When implementing a nested interface, we refer to it as i_first. i_second, where i_first is the name of the interface in which the interface is nested and i_second is the interface's name.
There is another nested interface which is nested inside a class its syntax is as follows:
class c_name{
interface i_name{
...
}
}
When implementing a nested interface, we refer to it as c_name. i_name, where c_name is the name of the class in which the interface is nested and i_name is the interface's name.
Example 1: Let us have a look at the following code of nested interface
//Driver Code Starts
import java.util.*;
// Parent Class
//Driver Code Ends
class Parent {
// Nested Interface
interface Test {
void show();
}
}
// Child Class
class Child implements Parent.Test {
public void show()
{
//Driver Code Starts
System.out.println("show method of interface");
}
}
class Geeks
{
public static void main(String[] args)
{
// instance of Parent class with Nested Interface
Parent.Test obj;
// Instance of Child class
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output
show method of interface
Explanation: The access specifier of the nested interface Test is package-private (default) since no access modifier is specified. We can also assign public, protected or private access specifiers to nested interfaces inside a class.
Example 2: Below is an example of protected Nested Interface
//Driver Code Starts
import java.util.*;
class Parent {
protected interface Test {
//Driver Code Ends
void show();
}
}
class Child implements Parent.Test {
public void show(){
System.out.println("show method of interface");
}
}
// Driver Class
//Driver Code Starts
class Geeks
{
public static void main(String[] args)
{
Parent.Test obj;
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output
show method of interface
Explanation: In the above example, if we change the access specifier to private, it will cause a compilation error because the derived class Child tries to access a private interface.
Interface Nested Inside Another Interface
An interface can be declared inside another interface also. We mention the interface as Parent.Test where Parent is the name of the interface in which it is nested and Test is the name of the interface to be implemented.
Example 1: Working of interface inside another interface
//Driver Code Starts
import java.util.*;
// Nested Interface-Interface
//Driver Code Ends
interface Parent {
interface Test {
void show();
}
}
class Child implements Parent.Test {
public void show() {
System.out.println("show method of interface");
}
}
//Driver Code Starts
// Main Class
class Geeks
{
public static void main(String[] args)
{
Parent.Test obj;
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output
show method of interface
Explanation: In the above example, when we put an interface inside another interface it is automatically public and static even if we do not write public and if we try to make it private and protected, the compiler will give an error. Everything inside an interface is always considered public by default.
Example 2: Interface cannot have non-public member interface
//Driver Code Starts
import java.util.*;
interface Parent {
//Driver Code Ends
protected interface Test {
void show();
}
}
class Child implements Parent.Test {
public void show()
{
System.out.println("show method of interface");
}
}
//Driver Code Starts
class Geeks
{
public static void main(String[] args)
{
Parent.Test obj;
Child t = new Child();
obj = t;
obj.show();
}
}
//Driver Code Ends
Output:
Example 3: NestedInterface inside the class
//Driver Code Starts
public class Geeks {
//Driver Code Ends
// Nested interface
public interface NestedInterface {
public void nestedMethod();
}
public static void main(String[] args)
{
// Implement nested interface
NestedInterface nested = new NestedInterface() {
public void nestedMethod()
{
System.out.println(
"Hello from nested interface!");
}
};
// Call nested interface method
nested.nestedMethod();
}
}
Output
Hello from nested interface!
Explanation: In this example, we have a nested interface NestedInterface inside the outer class. We then implement the interface using an anonymous inner class in the main method and call its method nestedMethod(). This is just one way to use nested interfaces in Java.
Uses of Nested Interfaces
In Java, nested interfaces can be used for a variety of purposes, including:
- When we put one interface inside another interface it makes the code more organized and easy to understand as well.
- If we nest an interface inside a class, it limits where that interface can be used. This helps keep our code safer and reduces the chances because the interface wonât be accessible everywhere.
- Nested interfaces are great for callbacks. This means one object can pass itself to another and the second object can call a method defined inside the nested interface.
- By using nested interfaces, we can set up a contract. Different classes can follow this contract by implementing the same interface but with their own versions