Hey, Great question set for every chapter.
I was trying to solve question 5 from Lambda chapter(Chapter-9) for the following question
interface AnInterface {
default int aMethod() { return 0; }
int anotherMethod();
}
public class Question_9_5 implements AnInterface {
public static void main(String[] args) {
AnInterface a = () -> aMethod();
System.out.println(a.anotherMethod());
}
@Override
public int anotherMethod() {
return 1;
}
}
and found this as the answer key
The correct answer is C.
A lambda expression cannot access the default methods of its functional interface (aMethod() in this case).
Since Question_9_4 implements AnInterface, this class does have that method, but it can be accessed from main() because it's a static method.
As per my understanding, the answer key should read this,
A lambda expression can access the default methods of its functional interface (aMethod() in this case), since default methods are inherited. But it cannot be accessed from main() because it's a static method and has no this reference.
Hey, Great question set for every chapter. I was trying to solve question 5 from Lambda chapter(Chapter-9) for the following question
and found this as the answer key
As per my understanding, the answer key should read this,
A lambda expression can access the default methods of its functional interface (aMethod() in this case), since default methods are inherited. But it cannot be accessed from main() because it's a static method and has no this reference.
Please let me know your opinion.