Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to turn an existing interface into a functional interface?

I am using an interface that looks something along the lines of this:

public interface ObjectListener {
    public void objectAdded(Object o);
    public void objectRemoved(Object o);
}

And I am currently using an anonymous class to implement the interface, but I don't care about one of the two methods. Something along the lines of this:

someObject.addListener(new ObjectListener() {

    @Override
    public void objectAdded(Object o) {
        doSomething(o);
    }

    @Override
    public void objectRemoved(Object o) {}
});

Now, I've been using the new lambda expressions in Java 8 wherever I'm able, and I would like to use the added simplicity in a situation like this. After all, I'm only implementing one of the methods, but since there are two methods in the interface, I can't use it in a lambda expression.

Is there a way for me to get around this restriction?

like image 976
Jazzer Avatar asked Apr 25 '14 04:04

Jazzer


People also ask

Can you create your own functional interface?

The functional interface is a simple interface with only one abstract method. A lambda expression can be used through a functional interface in Java 8. We can declare our own/custom functional interface by defining the Single Abstract Method (SAM) in an interface.

What do you need to make an interface a functional interface?

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.

What makes an interface functional?

An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class. Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces.

Can an interface extend functional interface?

Interface can extend another interface and in case the Interface it is extending in functional and it doesn't declare any new abstract methods then the new interface is also functional.


1 Answers

In order to reuse an existing interface that is not a functional interface in a lambda expression, you must also use the new Java8 feature, default methods.

In this case, if you wanted to use a lambda expression in place of the anonymous class, you would have to do the following.

First, you need to redefine the ObjectListener as a new interface:

public interface ObjectAddedListener extends ObjectListener {
    @Override
    default public void objectRemoved(Object o) {}
}

We just simply add an empty default implementation to the method that we don't care about, which leaves the objectAdded() method as the sole abstract method in the interface.

Then you can use the new type in place of any ObjectListener, and since there is only one method without an implementation in the new interface, you can use it in lambda expressions, like so:

ObjectAddedListener listener = o -> doSomething(o);
someObject.addListener(listener);

Note that if you wanted to use this new type directly in the addListener() method you would first need to cast the lambda expression as the newly defined type like so:

someObject.addListener((ObjectAddedListener) o -> doSomething(o));
like image 83
Jazzer Avatar answered Oct 30 '22 03:10

Jazzer