Consider functional interfaces A
and B extends A
. Is it possible to create an instance implementing not only A
but also B
with a lambda expression when only A
is required?
Example:
interface A {
void foo();
}
interface B extends A {}
void bar(A arg) {
System.out.println(arg instanceof B);
}
/* somewhere in a block */
bar( () -> {} ); // Prints "false"
How could I call bar(A)
so that it prints true
?
I would find this useful in event listeners, where subinterfaces could specify additional data about the listener, e.g. if a Listener
also implements ConcurrentListener
, it is invoked concurrently.