Nested Interface in Java

An interface can have another interface which is known as a nested interface.


\begin{lstlisting}
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
\end{lstlisting}

Example of nested interface which is declared within the interface


\begin{lstlisting}
interface Showable{
void show();
interface Message{
voi...
...estNestedInterface1();//upcasting here
message.msg();
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
hello nested interface
\end{lstlisting}

Let's see how we can define an interface inside the class and how we can access it. Below program is the example for nested interface which is declared within the class.


\begin{lstlisting}
class A{
interface Message{
void msg();
}
}
\par
clas...
...estNestedInterface2();//upcasting here
message.msg();
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
hello nested interface
\end{lstlisting}