Default Methods
Java 8 also introduced default methods: non-static, concrete methods defined in interfaces.
- A default method is declared with the keyword
defaultand must have a method body. - A default method is inherently
public(like all interface methods).
public interface Fillable {
double LITERS_PER_GALLON = 3.78541; // public static final field
void fill(int amount);
static double litersFromGallons(double gallons) {
return gallons * LITERS_PER_GALLON;
}
default int getCapacityLiters() {
return 0;
}
}
Default methods allow the author of an interface to add new methods without breaking the contract of existing implementor classes and sub-interfaces.
Drill¶
Which of these are valid declarations in an interface?
- [ ]
default void doStuff1();- [ ]
public void doStuff2();- [ ]
static void doStuff3();- [ ]
default static void doStuff4() { System.out.println("Doing stuff."); }- [ ]
static void doStuff5() { System.out.println("Doing stuff"); }(Solution: InterfaceMethodDrill.java)