Can we declare an Interface Within Another Interface in Java

In Java it is possible to declare an Interface Within Another Interface.

  • There we are created an interface Test1 which contains another interface Test2.
  • From a InterfaceMain class to implement Test1 and Test2 interface, also and providing body for all abstract methods and variable.
  • Here we declare Test1.test2 where Test1 is parent interface and Test2 child interface.
interface Test1 {
	int i = 18;

	interface Test2 {
		public void hello();

		int j = 10;
	}
}

public class InterfaceMain implements Test1.Test2, Test1 {
	public void hello() {
		System.out.println("calling interface test2 method");
	}

	public static void main(String[] args) {
		InterfaceMain iMain = new InterfaceMain();
		iMain.hello();
		System.out.println("interface test1 value:- " + i);
		System.out.println("interface test2 value:- " + j);
	}
}

Output :-
calling interface test2 method
interface test1 value:- 18
interface test2 value:- 10