FunctionalInterface
An interface with one abstract method declaration is known as Functional Interface.
eg:
public interface ExampleIfc {
void showExample();
}
An interface with more than one abstract methods are not considered as functional
interfaces.
eg:
Not a functional interface
public interface ExampleIfc {
void showExample();
boolean executeExample();
}
. FunctionalInterface Annotation type:
FunctionalInterface annotation type (@FunctionalInterface) is a marker interface.
Note: A marker interface is one which indicates or hints the compiler.
using this annotation type on an interface makes the compiler verify whether
the declared interface contains one and only one abstract method.
Note: you must not use this annotation type on
CLASSES
ANNOTATION TYPES
ENUMS
If used, yields a compile-time error.
. Can be used only on the interfaces with one abstract method.
LEGAL :
@FunctionalInterface
public interface Runner {
void run();
}
This compiles fine.
ILLEGAL:
@FunctionalInterface
public interface ExampleIfc {
void showExample();
boolean executeExample();
}
Compile-time error : because two abstract methods.
ILLEGAL
@FunctionalInterface
public class XyzClass {
public void xyzMethod() {
....
}
}
Compile-time error : because annotation type cannot be applied on classes.
Note: The concept of the Functional interface is very important to learn about the Java 8 lambda expressions.
Hope this is helpful
If you like this, please share.
An interface with one abstract method declaration is known as Functional Interface.
eg:
public interface ExampleIfc {
void showExample();
}
An interface with more than one abstract methods are not considered as functional
interfaces.
eg:
Not a functional interface
public interface ExampleIfc {
void showExample();
boolean executeExample();
}
. FunctionalInterface Annotation type:
FunctionalInterface annotation type (@FunctionalInterface) is a marker interface.
Note: A marker interface is one which indicates or hints the compiler.
using this annotation type on an interface makes the compiler verify whether
the declared interface contains one and only one abstract method.
Note: you must not use this annotation type on
CLASSES
ANNOTATION TYPES
ENUMS
If used, yields a compile-time error.
. Can be used only on the interfaces with one abstract method.
LEGAL :
@FunctionalInterface
public interface Runner {
void run();
}
This compiles fine.
ILLEGAL:
@FunctionalInterface
public interface ExampleIfc {
void showExample();
boolean executeExample();
}
Compile-time error : because two abstract methods.
ILLEGAL
@FunctionalInterface
public class XyzClass {
public void xyzMethod() {
....
}
}
Compile-time error : because annotation type cannot be applied on classes.
Note: The concept of the Functional interface is very important to learn about the Java 8 lambda expressions.
Hope this is helpful
If you like this, please share.
No comments:
Post a Comment