Consider following method



default void print(){
}

Which statement is true?

A. This method is invalid.
B. This method can be used only in an interface.
C. This method can return anything.
D. This method can be used only in an interface or an abstract class.
E. None of above.

題解

Java 8的介面(interface)可以加入已經實作好程式的預設方法(default method)和靜態方法(static)。預設方法使用「default」來修飾,只能給實體化出來的物件來呼叫使用;靜態方法使用「static」來修飾,只能給介面來呼叫使用。