Nested Interface

  • 0
     Interfaces in Java,  just like classes  can be nested. An interface declared inside body of another interface is called nested interface. However, the two interfaces exists independently in terms of implemention. It means that one does not needs to implement the methods of both the interface if one implements only one of them. However, inner interface cant exist on its own,
Here, is the interface that has an inner interface. It should be named as Print.java.
package InterfaceIllustraion;

interface Print
{
 void Printme();

 interface Printme
 {
  void Printmetoo();
 }
}
In the above source, Print is the outer interface and PrintMe is an inner interface. The program ItPrints.Java implements the Printme interface.
package InterfaceIllustraion;

class PrintsSomething implements Print.Printme
{
 public void Printmetoo()
 {
  System.out.println("Inside Printmetoo function");
 }
 
}
class ItPrints
{
 public static void main(String...args)
 {
  PrintsSomething ps = new PrintsSomething();
  ps.Printmetoo();
 }
}
To compile the program
javac -d . Print.java
javac -d . ItPrints.java
And to run
java InterfaceIllustration.ItPrints
Here is the output that I received on my machine.
Inside Printmetoo function

No comments:

Post a Comment