Java Reference
In-Depth Information
class). An interface is still a functional interface if it has many default methods as long as it
specifies
only one abstract method
.
To check your understanding,
Quiz 3.2
should let you know if you grasp the concept of a
functional interface.
Quiz 3.2: Functional interface
Which of these interfaces are functional interfaces?
public interface Adder{
int add(int a, int b);
}
public interface SmartAdder extends Adder{
int add(double a, double b);
}
public interface Nothing{
}
Answer:
Only Adder is a functional interface.
SmartAdder isn't a functional interface because it specifies two abstract methods called add (one
is inherited from Adder).
Nothing isn't a functional interface because it declares no abstract method at all.
What can you do with functional interfaces? Lambda expressions let you provide the
implementation of the abstract method of a functional interface directly inline and
treat the
whole expression as an instance of a functional interface
(more technically speaking, an
instance of a
concrete implementation
of the functional interface). You can achieve the same
thing with an anonymous inner class, although it's clumsier: you provide an implementation
Search WWH ::
Custom Search