Functional Interface(FI):-
Example 1.
@FunctionalInterface
interface A {
public void m1();
default void m2();
public static void m3()
{
}
}
Example 2. This code gives compilation error.
@FuncationalInterface
interface A
{
public void test1();
public void test2();
}
Above example Inside Functional Interface we can take only one abstract method, if we take more than one abstract method then compiler raise an error message that is called we will get compilation error.
Example 3.
If an interface extends Functional Interface and child interface doesn’t contain any abstract method then child interface is also Functional Interface
@Functional Interface
interface A {
public void test();
}
@Functional Interface
Interface B extends A
{ }
Example 4. In the child interface we can define exactly same parent interface abstract method.
@Functional Interface
interface A {
public void testOne();
}
@Functional Interface
interface B extends A {
public void testOne();
}
Output :- Compile Successfully.
Example 5.
In the child interface we can’t define any new abstract methods otherwise child interface won’t be Functional Interface and if we are trying to use @Functional Interface annotation then compiler gives an error message.
@Functional Interface {
interface A {
public void testOne();
}
@Functional Interface
interface B extends A
{
public void testTwo();
}
Output:- Compile time Error.
Example 6.
@FunctionalInterface
interface A
{
public void testOne();
}
interface B extends A
{
public void testTwo();
}
No compile time error
In the above example in both parent & child interface we can write any number of default methods and there are no restrictions. Restrictions are applicable only for abstract methods.
Example 7. Without Lamda Expression.
interface interf{
public void m1();
}
class Demo implements interf{
@Override
public void m1() {
System.out.println("m1() Method implementation");
}
}
public class WithoutLambdaTest {
public static void main(String[] args) {
interf in = new Demo();
in.m1();
}
}
Output:- 9
Example 10. With Lambda Expression
interface interTest{
public int square(int n);
}
public class Test2WithLambda {
public static void main(String[] args)
{
interTest test = n -> n*n;
System.out.println("square is :"+test.square(3));
}
}
Output:- 9
Example 11. With Lambda Expression
interface Interfm
{
public void m1(int i);
}
public class Test3WithLambda
{
public static void main(String[] args)
{
Interfm in = (i)-> System.out.println(i*i);
in.m1(10);
}
}
Example 12. Without Lambda Expression using Thread
class MyRunnable implements Runnable{
@Override
public void run() {
for(int i=0; i<10; i++){
System.out.println("Child Thraed");
}
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread t= new Thread(mr);
t.start();
for(int i=0; i< 10; i++){
System.out.println("Main Thread");
}
}
}
public class ThreadDemoWithLambda {
public static void main(String[] args) {
Runnable r = ()->{
for(int i=0; i<10; i++){
System.out.println("Child Thread");
}
};
Thread t = new Thread(r);
t.start();
for(int i=0; i<10; i++){
System.out.println("Main Thread");
}
}
}
Example :14. How to Convert Fahrenheit to Celsius using Lambda Expression.
Output:
37.0
4.970969537898672
Functional Interface contains single abstract method is call functional interface or can have any number of default method or can contains any number of static method.
- Runnable. è run()
- Comparable è compareTo()
- ActionListener è actionPerformed()
- Callable è call()
Default Method and Static method
Note:
- Functional Interface can contains single abstract method cannot allow multiple abstract method.
- Functional interface can contains any number of default method or static method.
- Java 8, Sun Micro System introduced @Functional Interface annotation to specify that the interface is Functional Interface.
@FunctionalInterface
interface A {
public void m1();
default void m2();
public static void m3()
{
}
}
Note:
This example interface contains single abstract method, one default method and one static method.
Functional interface with receipt to inheritance:
- Parent interface is Functional interface then child interface is also functional interface
- Parent interface method automatically available in child interface. It is not provide the method through user
@FuncationalInterface
interface A
{
public void test1();
public void test2();
}
Above example Inside Functional Interface we can take only one abstract method, if we take more than one abstract method then compiler raise an error message that is called we will get compilation error.
Example 3.
If an interface extends Functional Interface and child interface doesn’t contain any abstract method then child interface is also Functional Interface
@Functional Interface
interface A {
public void test();
}
@Functional Interface
Interface B extends A
{ }
Example 4. In the child interface we can define exactly same parent interface abstract method.
@Functional Interface
interface A {
public void testOne();
}
@Functional Interface
interface B extends A {
public void testOne();
}
Output :- Compile Successfully.
Example 5.
In the child interface we can’t define any new abstract methods otherwise child interface won’t be Functional Interface and if we are trying to use @Functional Interface annotation then compiler gives an error message.
@Functional Interface {
interface A {
public void testOne();
}
@Functional Interface
interface B extends A
{
public void testTwo();
}
Output:- Compile time Error.
Example 6.
@FunctionalInterface
interface A
{
public void testOne();
}
interface B extends A
{
public void testTwo();
}
No compile time error
In the above example in both parent & child interface we can write any number of default methods and there are no restrictions. Restrictions are applicable only for abstract methods.
Example 7. Without Lamda Expression.
interface interf{
public void m1();
}
class Demo implements interf{
@Override
public void m1() {
System.out.println("m1() Method implementation");
}
}
public class WithoutLambdaTest {
public static void main(String[] args) {
interf in = new Demo();
in.m1();
}
}
Output:- m1() Method implementation
Example 8. With Lambda Expression.
interface Interfa{
public void m1();
}
public class WithLambdaTest {
public static void main(String[] args) {
Interfa in =()->System.out.println("m1() Method implementation");
in.m1();
}
}
Output :- m1() Method implementation
Example 9. Without Lambda Expression
interface interA{
public int square(int n);
}
class Demosq implements interA{
@Override
public int square(int n) {
return n * n;
}
}
public class Test2WithoutLambda {
public static void main(String[] args) {
interA a = new Demosq();
System.out.println(a.square(3));
}
}
Output:- 9
Example 10. With Lambda Expression
interface interTest{
public int square(int n);
}
public class Test2WithLambda {
public static void main(String[] args)
{
interTest test = n -> n*n;
System.out.println("square is :"+test.square(3));
}
}
Output:- 9
Example 11. With Lambda Expression
interface Interfm
{
public void m1(int i);
}
public class Test3WithLambda
{
public static void main(String[] args)
{
Interfm in = (i)-> System.out.println(i*i);
in.m1(10);
}
}
Output:-100
Example 12. Without Lambda Expression using Thread
class MyRunnable implements Runnable{
@Override
public void run() {
for(int i=0; i<10; i++){
System.out.println("Child Thraed");
}
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread t= new Thread(mr);
t.start();
for(int i=0; i< 10; i++){
System.out.println("Main Thread");
}
}
}
Example 13. With Lambda Expression using Thread
public class ThreadDemoWithLambda {
public static void main(String[] args) {
Runnable r = ()->{
for(int i=0; i<10; i++){
System.out.println("Child Thread");
}
};
Thread t = new Thread(r);
t.start();
for(int i=0; i<10; i++){
System.out.println("Main Thread");
}
}
}
Example :14. How to Convert Fahrenheit to Celsius using Lambda Expression.
Output:
37.0
4.970969537898672
0 Comment to "Functional Interface"
Post a Comment