We had talked about interfaces and their fully / partial implementations. We also talked about Nested Interface in a Class (declaring an interface in a class) and Nested Class in an Interface (declaring a class in am interface). Let's declare an interface inside an interface (Nested Interface in an Interface) -
The inner interface class instance can be used three ways -
- instance methods can be called directly
- instance is passed to interface variable by reference and then methods are called through that variable
- instance is passed to the outer interface method which takes a parameter interface variable
All three examples are shown in the code given below -
// Outer Interface
public interface OuterInterface {
public void display();
public void showInner(InnerInterface i);
// Inner Interface
public interface InnerInterface {
public void display();
}
}
// Implementation of Outer Interface
public class OuterInterfaceClass implements OuterInterface {
public void display() {
System.out.println("Display method from OuterInterfaceClass");
}
public void showInner(InnerInterface i) {
System.out.println("Show method from OuterInterfaceClass");
i.display();
}
}// Implementation of Inner Interface
public class InnerInterfaceClass implements OuterInterface.InnerInterface {
public void display() {
System.out.println("Display method from InnerInterfaceClass");
}
}// Demo class showing accessing inner interface in all three ways
public class Test {
public static void main (String args[]) {
// Creates a an object of class implementing OuterInterface
OuterInterfaceClass oic = new OuterInterfaceClass();
oic.display();
System.out.println("___________________________________________________");
// Creates a an object of class implementing InnerInterface
InnerInterfaceClass iic = new InnerInterfaceClass();
iic.display();
System.out.println("___________________________________________________");
// Creates a a variable of inner interface and assign the class variable By Reference
OuterInterface.InnerInterface oiii;
oiii = iic;
oiii.display();
System.out.println("___________________________________________________");
// Calls the outer interface method which accesses inner interface
oic.showInner(iic);
}
}
Let's explore all three options -
InnerInterfaceClass is created by implementing inner interface InnerInterface directly.
// Implementation of Inner Interface
public class InnerInterfaceClass implements OuterInterface.InnerInterface {
public void display() {
System.out.println("Display method from InnerInterfaceClass");
}
}
1. instance methods can be called directly -
Create an instance of class implementing inner interface and then call interface methods:
// Creates a an object of class implementing InnerInterface
InnerInterfaceClass iic = new InnerInterfaceClass();
iic.display();
2. instance is passed to an interface variable by reference and then methods are called through that variable -
Declare a variable of inner interface and assign the inner class variable (Using class reference), call the method through interface variable:
// Creates a a variable of inner interface and assign the class variable By Reference
OuterInterface.InnerInterface oiii;
oiii = iic;
oiii.display();
3. instance is passed to the outer interface method which takes a parameter interface variable -
Creates an instance of class implementing outer interface and then call interface methods passing inner interface as a parameter:
// Creates a an object of class implementing OuterInterface
OuterInterfaceClass oic = new OuterInterfaceClass();
// Calls the outer interface method which accesses inner interface
oic.showInner(iic);
Here is the output of the above code -
Display method from OuterInterfaceClass
___________________________________________________
Display method from InnerInterfaceClass
___________________________________________________
Display method from InnerInterfaceClass
___________________________________________________
Show method from OuterInterfaceClass
Display method from InnerInterfaceClass
Code can be accessed on Github Link.