1: //InterfaceJava8.java 3: public interface InterfaceJava8 4: { 5: int INTEGER_CONSTANT = 10; 7: //Prior to Java 8 (note the [redundant] "abstract" keyword) 8: public abstract void legacyMethod(); 9: //A method like this must be implemented in 10: //any class that implements this interface. 12: //Only in Java 8 (note "static" keyword) 13: public static void staticMethod() 14: { 15: System.out.println("Printing from static method"); 16: } 18: //Only in Java 8 (note "default" keyword) 19: public default void defaultMethod() 20: { 21: System.out.println("Printing from default method"); 22: } 23: } 24: /* 25: The new static and default methods work together with the usual 26: (legacy) abstract methods in an interface to deliver the full 27: functionality of an interface. 29: Advice for placing a static method implementation in an interface: 30: If you need a static utility method for the implementors of an 31: interface, add that static utility method to the interface itself 32: as a static method. 34: Advice for placing a default method implementation in an interface: 35: If a feature is added to an existing hierarchy of classes and that 36: feature is not needed for all the classes in the hierarchy, place 37: that "feature" in a default method. If the method were made abstract, 38: it would have to be implemented by all the classes in the hierarchy. 39: This would break all code that implemented the interface. Making the 40: method a default one allows any interface implementation which wants 41: to use it to do so, while other implementations can just ignore it, 42: or override it if it doesn't do quite what we want it to do. This 43: makes the functionality implemented via the default method an optional 44: feature rather than a mandatory one. 45: */