eh3rrera / ocpj8-book

Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam (1Z0-809)
Other
129 stars 90 forks source link

Typo in Lambda Chapter-Answer key #71

Closed protyay closed 6 years ago

protyay commented 6 years ago

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

  1. 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.

Please let me know your opinion.

eh3rrera commented 6 years ago

You're right, I got it wrong.

It's now fixed. Thanks a lot 👍

protyay commented 6 years ago

Cheers! Waiting for more great contents from you

eh3rrera commented 6 years ago

I don't have anything planned yet, but we'll see next year. Thanks!