Functional interface concept

When I take a look at lambda expressions, the book touches on a functional interface, which has only one abstract method. My problem is regarding this quiz question.

/* Which of these interfaces are functional interfaces? */
public interface Adder{
   int add(int a, int b);
}
public interface SmartAdder extends Adder{
   int add(double a, double b);
}
public interface Nothing{
}

      

I know the latter is not there, but I think the first and second should be a functional interface. But the book says the second is not. What for? Doesn't override the method add

? So even in the second, isn't there just one abstract method?

+3


source to share


4 answers


An easy way to search would be to try to identify a class that implements SmartAdder

. The compiler will tell you that you need to implement both add(int, int)

and add(double, double)

.

Obviously, you thought you would add(double, double)

override add(int, int)

, but they are actually separate methods and could potentially have completely unrelated implementations.

If SmartAdder

defined an implementation default

add(int, int)

, it will be a functional interface:



public interface SmartAdder extends Adder {
   int add(double a, double b);

   default int add(int a, int b) {
     return add((double)a, (double)b); // this calls the double method instead
  }
}

      

You may have come across annotation as well @FunctionalInterface

- this can be placed on an interface for compile-time enforcement, the interface has exactly one abstract method. If it SmartAdder

was annotated with @FunctionalInterface

, the interface itself will not compile.

+4


source


SmartAdder has two methods. The method signatures are different. A functional interface can only have one method.



+1


source


interface SmartAdder

overloads rather than overrides interface Adder

. This is because the name has the same name, but the parameter types are different. Hence, it has 2 functions. To be a functional interface, it must have only one function.

==> Only interface Adder

a functional interface.

+1


source


In Java, a method in a subtype overrides a method of the parent type when it has the same signature. The signature means the name and arguments of the method. In particular, the arguments must be of the same exact type and must be declared in the same order in both methods, i.e. The argument types of a method declared in a subtype cannot be subtypes or types wider than the types of arguments declared in a method of the parent type.

So, in your interface, SmartAdder

the signature method add(double a, double b)

does not override the method of add(int a, int b)

your interface Adder

because it is double

wider int

. When a type has two or more methods with the same name but different arguments, it is called method overloading and is completely different from method overriding.

This is why it SmartAdder

ends up having two abstract methods, hence it is not a functional interface (which requires a type to have only one abstract method).

+1


source







All Articles