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: 10036
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: 10036
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: 10036
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

oscarcito
Posts: 4
Joined: Wed Jan 18, 2023 3:59 am
Contact:

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

Post by oscarcito »

Hi!
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]

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

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

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

oscarcito
Posts: 4
Joined: Wed Jan 18, 2023 3:59 am
Contact:

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

Post by oscarcito »

I have checked it and it works

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 36 guests