Bad news ! KnowledgeBlackBelt will definitely shut down on Jan 31st 2013.
Content is available for free ...
$ play new my-app
$ cd my-app
$ play idea
A functional interface is an interface that has just one abstract method, and thus represents a single function contract. (In some cases, this "single" method may take the form of multiple abstract methods with override-equivalent signatures (8.4.2) inherited from superinterfaces; in this case, the inherited methods logically represent a single method.)
In addition to the usual process of creating an interface instance by declaring and instantiating a class, instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
The function descriptor of a functional interface I is a method type—type parameters, formal parameter types, return types, and thrown types—that can be used to legally override the abstract method(s) of I.
package fr.dr.practice;Main.java
/**
* Interface with just one abstract method.
*/
public interface ICode {
String generate(int codeNumber);
}
package fr.dr.practice;
/**
* Created with IntelliJ IDEA.
*/
public class Main {
public static String generateTmpCode(int newCode) {
ICode code = new ICode() {
@Override
public String generate(int codeNumber) {
return "MAIN_" + codeNumber + Math.random();
}
};
return code.generate(newCode);
}
public static String generateTmpCodeWithLambda(int newCode) {
ICode icode = codeNumber -> "MAIN_" + codeNumber + Math.random();
return icode.generate(newCode);
}
public static void main(String[] args) {
System.out.println(Main.generateTmpCode(12));
System.out.println(Main.generateTmpCodeWithLambda(12));
}
}
More complex examples
In some cases, this "single" method may take the form of multiple abstract methods with override-equivalent signatures (8.4.2) inherited from superinterfaces; in this case, the inherited methods logically represent a single method.
public interface ICode {
String generate(int codeNumber);
boolean equals(Object obj);
}
public interface ICode {
String generate(int codeNumber);
Object clone();
}
interface X { void m() throws IOException; } interface Y { void m() throws EOFException; } interface Z { void m() throws ClassNotFoundException; } interface XY extends X, Y {} interface XYZ extends X, Y, Z {} // XY has descriptor ()->void throws EOFException // XYZ has descriptor ()->void (throws nothing)