CJC By Kunal Sir [FUNCTIONAL INTERFACE]
Functional interface
1)Interface which have only one abstract method that is call functional interface
2) Functional interface is also call Single abstract method interface(SAM interface)
3)@FunctionalInterface Annotation provide in jdk 8 inside [Link] packagen
4)if any interface annoted with @FunctionInterface then that interface cannot be more then one
abstract method if we try to write then it will give compile time error
5)if we do not mark interface with @FunctionalInterface and that interface have only one abstract
method then that interface is also call functional interface
6)inside functional interface we can write any number of default and static method
7) If functional interface declares an abstract method overriding one of the public methods of Java
Object class, that will also not be counted.
8) The instances of functional interface can be created by using lambda expressions, method
references, or constructor references.
9)A new package [Link] has been added with bunch of functional interfaces for lamda
Expression and method reference
10)if interface extends functional interface and doesn't declare any abstract method then the new
interface is also call functional interface
Example-1
package [Link];
@FunctionalInterface
public interface I {
void m1();
}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]
package [Link];
public class A implements I{
@Override
public void m1() {
[Link]("m1---A");
package [Link];
public class Test {
public static void main(String[] args) {
I i=new A();
i.m1();
Example 2
by using Anonymous class
package [Link];
@FunctionalInterface
public interface I {
void m1();
}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]
package [Link];
public class Test {
public static void main(String[] args) {
I i=new I(){
public void m1()
[Link]("m1---Annonumius");
};
i.m1();
Example-3
by using Lambda Expression
package [Link];
@FunctionalInterface
public interface I {
void m1();
}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]
package [Link];
public class Test {
public static void main(String[] args) {
I i=()->{[Link]("m1---Lambda"); };
i.m1();
Example -4
Runnable Interface
package [Link];
public class MyRunnable implements Runnable{
@Override
public void run() {
[Link]("run--method of
MyRunnable");
}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]
package [Link];
public class Test {
public static void main(String[] args) {
MyRunnable mr=new MyRunnable();
Thread t=new Thread(mr);
[Link]();
}
------------------------------
Example-5
By using Anonymus class
package [Link];
public class Test {
public static void main(String[] args) {
Runnable r=new Runnable() {
@Override
public void run() {
[Link]("run--
anonymus class");
}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]
};
Thread t=new Thread(r);
[Link]();
Example -6
By using Lambda Expresion
package [Link];
public class Test {
public static void main(String[] args) {
Runnable r=()-> [Link]("run-
-Lambda Expression");
Thread t=new Thread(r);
[Link]();
}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]