Page 1 of 1

About Question enthuware.ocpjp.v8.2.1863 :

Posted: Mon Apr 24, 2017 9:44 am
by jsousac
why the first solution is incorrect?

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

Posted: Mon Apr 24, 2017 10:00 am
by admin
First option is an abstract class but you need an interface to make it work.

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

Posted: Wed Sep 06, 2017 3:41 am
by rolandlensink
The explanation of the wrong answers are, let's say, not correct ... It must be an interface and not an abstract class !

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

Posted: Wed Sep 06, 2017 4:59 am
by admin
Could you please be specific? Which explanation do you think is incorrect and why?

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

Posted: Thu Jan 12, 2023 5:28 am
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?

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

Posted: Fri Jan 13, 2023 7:57 am
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.

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

Posted: Mon Jan 16, 2023 3:15 am
by yuir12
oh noted, thanks

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

Posted: Sat Apr 01, 2023 12:22 pm
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();
}

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

Posted: Sat Apr 01, 2023 12:35 pm
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.

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

Posted: Tue Apr 04, 2023 7:14 am
by oscarcito
I have checked it and it works