7

I was exploring features of Java 8 and came across "Functional Interface".

As per my understanding, these interfaces can have some default implemented methods as :

@FunctionalInterface
public interface ComplexFunctionalInterface extends SimpleFuncInterface 
{
    default public void doSomeWork()
    {
        System.out.println("Doing some work in interface impl...");
    }
    default public void doSomeOtherWork()
    {
        System.out.println("Doing some other work in interface impl...");
    }
}

But my doubt is, this what abstract class is for.

Why introduce functional interfaces.

codingenious
  • 8,385
  • 12
  • 60
  • 90

5 Answers5

15

But my doubt is, this what abstract class is for.

Why introduce functional interfaces.

Number of classes that can be extended: 1

Number of interfaces that can be implemented: more than 1

yamafontes
  • 5,552
  • 1
  • 18
  • 18
  • Is this the only reason? Java was very provide till jdk1.7 for not having multiple inheritance. – codingenious Nov 13 '13 at 09:45
  • You still don't have multiple inheritance. You only inherit from one source, but you can implement multiple interfaces - same as before. The inheritance graph is still a directed acyclic graph. – Markus Koivisto Nov 13 '13 at 09:48
  • But, by implementing multiple functional interface, won't diamond problem? – codingenious Nov 13 '13 at 09:55
  • @Batty apparently the diamond problem only kicks in if default function implementations are provided in the functional interface (also a new thing) - compiler solves this by forcing implementing classes to provide implementation of these methods when multiple default implementations are found in the interfaces. – demaniak Jul 06 '16 at 15:13
3

Functional interface are is used in a "safe" multiple inheritance. Differences:

  • A class may extend multiple functional interfaces.
  • Functional interfaces may have only a single abstract method.
  • Functional interfaces may not have fields unlike C++ abstract classes.

Typical usage is when you want to embed default functionality into objects. I.e. if you have a function-like object,

class MyFunction1 {
    public Integer apply(String s){
        ...
    }
}

class MyFunction2 {
    public List<String> apply(Integer s){
        ...
    }
}

And you want to make a composition out of them, you just drop in implements Function:

class MyFunction1 implements Function<String, Integer>{
    public Integer apply(String s){
        ...
    }
}

class MyFunction2 implements Function<Integer, List<String>>{
    public List<String> apply(Integer s){
        ...
    }
}

And you may create a composition of your functions. Two approaches compared:

No functional interfaces:

MyFunction1 myFunction1 = ...;
MyFunction2 myFunction2 = ...;

Function<String, List<String>> composition = (s) -> myFunction2.apply(myFunction1.apply(s));

With functional interfaces:

MyFunction1 myFunction1 = ...;
MyFunction2 myFunction2 = ...;

Function<String, List<String>> composition = myFunction1.andThen(myFunction2);

The difference

  • No need to re-implement functions.
  • Other functions available in the extending class: compose and identity.
  • New default function is made a part of a class hierarchy and there is no need to create a new object. Usually functions like compose() are not included into a class definition as it would result into class size growth. They are often put into separate utility classes. In Guava composition is put into a separate utility class Functions: Functions.compose. So with new functional interfaces you would not need to recall in which utility class your function is implemented.
user2891264
  • 90
  • 1
  • 10
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
  • what if both interface have method as "public Integer apply(String s)", won't this be a problem? – codingenious Nov 13 '13 at 10:16
  • If functional interfaces have the same signature for their abstract methods, this would result into compile-time error. I understand this as method inlining - one can't have duplicate methods in his classes. – Andrey Chaschev Nov 13 '13 at 10:23
1

Functional interfaces must have only one method. The only exception is mehtods declared in Object. See http://java.dzone.com/articles/introduction-functional-1

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

I have no experience with JAVA 8 yet, but from what I can see this would allow for a kind of multiple inheritance, which is not possible with abstract classes

DeltaLima
  • 5,864
  • 1
  • 16
  • 32
0

The interface Comparator is functional although it explicitly declares two methods, because only one is abstract; equals is an explicit declaration of a concrete method inherited from Object that, without this declaration, would otherwise be implicitly declared.

Shyam
  • 6,376
  • 1
  • 24
  • 38