About Question enthuware.ocpjp.v8.2.1863 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
jsousac
Posts: 1
Joined: Mon Apr 24, 2017 9:37 am
Contact:

About Question enthuware.ocpjp.v8.2.1863 :

Post by jsousac »

why the first solution is incorrect?

admin
Site Admin
Posts: 9804
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1863 :

Post by admin »

First option is an abstract class but you need an interface to make it work.
If you like our products and services, please help us by posting your review here.

rolandlensink
Posts: 7
Joined: Thu Mar 16, 2017 7:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1863 :

Post by rolandlensink »

The explanation of the wrong answers are, let's say, not correct ... It must be an interface and not an abstract class !

admin
Site Admin
Posts: 9804
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1863 :

Post by admin »

Could you please be specific? Which explanation do you think is incorrect and why?
If you like our products and services, please help us by posting your review here.

yuir12
Posts: 22
Joined: Thu Dec 09, 2021 11:23 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1863 :

Post by yuir12 »

For option #4, it says below
This is a valid functional interface and so the code will work fine. However, observe that the lambda expression in the code will capture the unread method (not the read method, because read method is not abstract). Therefore, r.read() will cause the read method defined in this interface to be invoked instead of the code implemented by the lambda expression.

Can we have a reference link where it supports r.read() can call abstract unread method of option #4, should it not call the same method signature?

admin
Site Admin
Posts: 9804
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1863 :

Post by admin »

In option 4, both read and unread methods have the same signature. But read is a default method while unread is not. So the given lambda expression:

Code: Select all

Reader r = b->{
    System.out.println("Reading book "+b.getTitle());  
};
will capture the unread method.

Next, the line of code books.forEach(x->r.read(x)); is calling the read method of the Reader object pointed to by r. Since the class that implements Reader (implemented by the above lambda) has a default read method, it will be invoked.

You can write the same code and confirm.
If you like our products and services, please help us by posting your review here.

yuir12
Posts: 22
Joined: Thu Dec 09, 2021 11:23 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1863 :

Post by yuir12 »

oh noted, thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests