I understand that a bunch of methods with body are added using default
keyword of Java 8. My question is where is this annotation useful? I see that only one abstract method is allowed to be added with such annotation. But Comparator has two abstract methods :
int compare(T o1, T o2);
boolean equals(Object obj);
Also , Please explain the use of this annotation? I see that its a runtime annotation, so what are its uses ?
From the documentation of FunctionalInterface
:
If an interface declares an abstract method overriding one of the public methods of
java.lang.Object
, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation fromjava.lang.Object
or elsewhere.
Since equals
is from java.lang.Object
, it's not counted. It's the compare
method that relates to FunctionInterface
.
It is useful since if you add it and you have more than one method in the interface you will get a compile error.
@FunctionalInterface
s are used to let Java know that this piece of code might be replaced with a lambda expression since every lambda is an implementation of some @FunctionalInterface
. It helps the compiler know what is the method signature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With