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: 10065
- 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: 10065
- 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: 10065
- 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
-
- Posts: 4
- Joined: Wed Jan 18, 2023 3:59 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
Hi!
I have a question. Can Interface2 be considered a functional interface?
I have a question. Can Interface2 be considered a functional interface?
Code: Select all
public interface Interface1 {
public void m1();
}
interface Interface2 extends Interface1 {
public void m1();
}
Last edited by admin on Sat Apr 01, 2023 12:27 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]
Reason: Please put code inside [code] [/code]
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
A functional interface must have exactly 1 abstract method. Think about it this way: if a concrete class were to implement the interface Interface2, how many methods would it have to implement? If only 1, then Interface2 is a functional interface, otherwise not.
Try writing a simple TestClass that implements Interface2 and see what happens when you compile it.
Try writing a simple TestClass that implements Interface2 and see what happens when you compile it.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Wed Jan 18, 2023 3:59 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1863 :
I have checked it and it works
Who is online
Users browsing this forum: Google [Bot] and 119 guests