About Question enthuware.ocpjp.v8.2.1863 :
Moderator: admin
-
- Posts: 1
- Joined: Mon Apr 24, 2017 9:37 am
- Contact:
About Question enthuware.ocpjp.v8.2.1863 :
why the first solution is incorrect?
-
- Site Admin
- Posts: 9804
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
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.
-
- Posts: 7
- Joined: Thu Mar 16, 2017 7:44 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
The explanation of the wrong answers are, let's say, not correct ... It must be an interface and not an abstract class !
-
- Site Admin
- Posts: 9804
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
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.
-
- Posts: 22
- Joined: Thu Dec 09, 2021 11:23 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
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?
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?
-
- Site Admin
- Posts: 9804
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
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:
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.
Code: Select all
Reader r = b->{
System.out.println("Reading book "+b.getTitle());
};
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.
-
- Posts: 22
- Joined: Thu Dec 09, 2021 11:23 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
oh noted, thanks
Who is online
Users browsing this forum: No registered users and 8 guests