Java8: About the functional interface

I would like to ask about the next piece of code related to the functional interface, for which I am new. I am confused by Rideable rider = Car :: new below. Does the instance Rideable (interface) or Car (class)? If it creates a Car object, the new Car() constructor (i.e., No arguments) should not exist, then how can this be allowed? Read the tutorial at https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html , but still could not understand.

 @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"); } } 
+5
source share
3 answers

You use the Lambda syntax to implement the getCar() method of the getCar() interface, where the following anonymous class program is omitted using the concise Lambda syntax:

 Rideable rideable = new Rideable() { @Override public Car getCar(String name) { return new Car(name); } }; 

This is Java 7 code. You can achieve the same using the Lambda expression:

 Rideable rider = name -> new Car(name); 

Or as in your example, using the method link:

 Rideable rider = Car::new; 

As a result, the getCar(String) method of the getCar(String) object can be used as new Car(String) .

And as the answer to your question, you create an instance of the Car class that implements the Rideable interface. This is another way to implement an interface without using the implement keyword.

If you think:

 Car auto = Car("MyCar")::new; 

or

 Car auto = Car::new; Car vehicle = auto::getCar("MyCar"); 

or

 Car vehicle = Rideable::new::getCar("MyCar"); 

All of these examples are wrong approaches. I have given you these examples because these are common mistakes that can be made when we talk about lambda expressions or a method.

+1
source

Does the instance create a Rideable (interface) or a car (class)?

Creates an instance (class that implements) the Rideable interface.

The Rideable functional interface has a single method, getCar , which takes a String argument and returns an instance of Car .

The public Car (String name) constructor takes a String argument and instantiates a Car .

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

And if that helps clarify the confusion, here the lambda expression is equivalent to the Car::new method reference:

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

or

 Rideable rider = s -> new Car(s); 
+5
source

You create a new "Car" object, which is a "Rideable". So yes, you are creating a new "Car" object.

Now Rideable is defined as "waiting for nameOfTheCar and gives you an instance of Car through the getCar() method", and this is exactly what "Car::new" does.

 Car::new 

in this example, the same as (as assigned by Rideable)

 carName -> new Car(carName) 

Now the above behavior completes using lambda, which will be Java7 looking something like this:

 Rideable rideable = new Rideable() { @Override public Car getCar(String carName ) { return new Car(carName); } }; 

Now let's say you have a different interface

 @FunctionalInterface interface SelfRideable { Car getSelfDriven(); } 

And now if you do

 SelfRideable selfDriven = Car::new; 

Then it will not compile, since now it has no parameter, this is its method ( getSelfDriven() ). To do this, you will need a default constructor.

+1
source

Source: https://habr.com/ru/post/1275012/


All Articles