Given:
interface Rideable {Car getCar (String name); }
class Car {
private String name;
public Car (String name) {
this.name = name;
}
}
Which code fragment creates an instance of Car?
Given:
interface Rideable {Car getCar (String name); }
class Car {
private String name;
public Car (String name) {
this.name = name;
}
}
Which code fragment creates an instance of Car?
The correct answer is the option where Rideable is assigned to Car::new, and then the getCar method is called with the argument 'MyCar'. This works because Rideable is a functional interface with a single abstract method, getCar. By using Car::new, we are effectively implementing the getCar method to return a new Car instance. Therefore, when rider.getCar('MyCar') is called, it creates a new Car object with the name 'MyCar'.
C is the Correct Answer
True...The interface must be functional
Why does it work? Rideable rider = Car::new; Class Car doesn't implement Interface Rideable! Can somebody explain it please
Rideable is a FunctionalInterface. Rideable rider = Car::new, assigns Car::new as rider's getCar() method. So when you call rider.getCar() you are calling Car::new.
C is the correct answer
Answer is C
Answer is C.