Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8: About Functional Interface

I would like to ask about the following piece of code related to Functional Interfaces. I am confused by:

Rideable rider = Car :: new

Is it creating a Rideable (interface) or Car (class) instance? If it is creating a Car object, the constructor new Car() (i.e. with no arguments) should be not existing, then how come this can be valid?

I Have been reading through this tutorial ,but still could not figure it out.

@FunctionalInterface 
interface Rideable {
  Car getCar (String name);
}

class Car {
  private String name;

  public Car (String name) {
    this.name = name;
  }
}

public class Test {
  public static void main(String[] args) {
    Rideable rider = Car :: new; 
    Car vehicle = rider.getCar("MyCar");
  }
}
like image 289
Patrick C. Avatar asked Jan 29 '18 09:01

Patrick C.


People also ask

What is the functional interface in Java 8?

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 is the benefit of functional interface in Java 8?

The major benefit is that we can pass a functional interface as an argument to another method and provide an implementation for these interfaces using lambda expressions. Java 8 Collections API has been rewritten and new Stream API is introduced that uses a lot of functional interfaces and Optional class.

What is purpose of functional interface in Java?

A functional interface has any number of default methods because they are not abstract and implementation already provided by the same. A functional interface declares an abstract method overriding one of the public methods from java. lang.

Do we have functional interfaces earlier to Java 8?

There were plenty of interfaces with a single abstract method even before Java 8. The term functional interface was introduced in Java 8. Runnable and Callable interfaces are commonly used in multithreaded applications. ActionListener interface is commonly used in Swing framework based applications when making GUIs.


1 Answers

Is it creating a Rideable (interface) or Car (class) instance?

It is creating an instance of (a class that implements) the Rideable interface.

The Rideable functional interface has a single method - getCar - that accepts a String argument and returns a Car instance.

The public Car (String name) constructor accepts a String argument and produces a Car instance.

Therefore the method reference Car::new (which in this case does not refer to a parameter-less constructor) can be used as implementation of the Rideable interface.

And if it helps to clarify the confusion, here's a lambda expression equivalent to the Car::new method reference:

Rideable rider = (String s) -> new Car(s);

or

Rideable rider = s -> new Car(s);
like image 90
Eran Avatar answered Sep 28 '22 07:09

Eran