I have an 'Animal' interface and 'Dog' class that implements 'Animal'.
public interface Animal {
void makeVoice();
}
public class Dog implements Animal {
@Override
public void makeVoice() {
System.out.println("aou aou");
}
}
In my main method, I wrote the following lines.
public class Main {
public static void main(String[] args) {
Animal dog = Dog::new; // How is it compiled? it should be Supplier<Animal>
dog.makeVoice(); // Doesn't call because 'dog' holds lambda expression (Supplier<Animal>) and not the actual 'Dog' class.
}
}
Why is it possible that Animal can hold Supplier?
I expected that it doesn't compile.
The constructor for
Dogsatisfies theAnimalfunctional interface. The parameter types match as bothmakeVoiceand the default constructor forDogtake no parameters. The constructor call is also void-compatible (it matches thevoidreturn type) because a class instance creation expression is an expression statement.The code is essentially equivalent to the following and it creates an implementation of
Animal, notSupplier:As an anonymous class, it would look like this: