Source of TestInterfaceJava8.java


  1: //TestInterfaceJava8.java

  3: public class TestInterfaceJava8 implements InterfaceJava8
  4: {
  5:     public static void main(String[] args)
  6:     {
  7:         InterfaceJava8.staticMethod(); //Implementation in the interface

  9:         TestInterfaceJava8 obj = new TestInterfaceJava8();
 10:         obj.defaultMethod(); //Implementation in the interface
 11:         obj.legacyMethod();  //Implementation given below

 13:         //We can refer directly to a constant defined in the interface:
 14:         System.out.println(INTEGER_CONSTANT);
 15:     }

 17:     public void legacyMethod() //Prior to Java 8
 18:     {
 19:         System.out.println("Printing from legacy method");
 20:     }

 22:     //If we don't like what defaultMethod() from InterfaceJava8 does, we can
 23:     //override it, like this:
 24:     /*
 25:         //Overrides the default method in the interface
 26:         public void defaultMethod()
 27:         {
 28:             System.out.println("Printing from the overridden default method");
 29:         }
 30:     */
 31: }
 32: /*  Output:
 33:     Printing from static method
 34:     Printing from default method
 35:     Printing from legacy method
 36:     10
 37: */