Sunday 29 June 2014

@FunctionalInterface

A Functional interface is the one which has only one abstract method. The method declared in functional interface must not a public member of Object class.

The annotation type FunctionalInterface is used to indicate that an interface is meant to be a functional interface.

@FunctionalInterface
interface Interface1{
 Object getObject();
 int getABC();
}

When you tries to compile the above interface , compiler throws below error. Since Interface1 contains two abstract methods.

Interface1.java:1: error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
  Interface1 is not a functional interface
    multiple non-overriding abstract methods found in interface Interface1
1 error

1. The following interface is not functional because it declares nothing which is not already a member of Object

@FunctionalInterface
 interface NonFunc {
 boolean equals(Object obj);
}

2. Below Program compiles fine. Since return types are covariantly equivalent, Interface2 Overrides the method in Interface1.
@FunctionalInterface
interface Interface1{
 Object getObject();
}

@FunctionalInterface
interface Interface2 extends Interface1{
 String getObject();
}

Related Links




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment