[HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

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

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

[HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by raphaelzintec »

hello,

i don't understand anything about predicated, i feel like this chapter miss parts.

Code: Select all

    public static boolean checkList(List list, Predicate<List> p){
        return  p.test(list);
    }

    public static void main(String[] args) {
        boolean b = checkList(new ArrayList<>(), (ArrayList al) -> al.isEmpty());
        System.out.println(b);
    }
and why this code is incorrect? in java like everything accept the subtype, here why it doesnt accept subclass type?

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

Re: [HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by admin »

I am not sure where are you seeing Predicate<List>. The section that you are referring to uses Predicate<Car>.

However, assuming that you really are interested in Predicate<List> and in knowing why (ArrayList al) -> al.isEmpty() can't implement Predicate<List>:

A lambda expression that implements Predicate<List> must satisfy the contract imposed by Predicate<List>, which means it must be able to transform to a class that implements the abstract method declared by Predicate<List> i.e. boolean test(List ). Something like this:

Code: Select all

class Lambda1 implements Predicate<List>{
  public boolean test(List l){ //correctly implements the method declared by Predicate<List>.
     return l.isEmpty();
  }
}
But the expression (ArrayList al) -> al.isEmpty() translates to:

Code: Select all

class Lambda2 implements Predicate<List>{
  public boolean test(ArrayList l){
     return l.isEmpty();
  }
}
But the Lambda2 class violates the rules of interface implementation. This is not a case of "passing" an ArrayList where a List is required. You are not "calling" the test(List ) method here, you are "implementing" the test(List ) method.

This has nothing to do with lambda and Predicate. It is about implementing an interface (any interface for that matter). For example, if you have an interface :

Code: Select all

interface X{
    void m(List l);
}
You cannot implement it like this:

Code: Select all

class CX implements X{ //will not compile because CX does not have implementation for X's method.
    public void m(ArrayList l){ //this does not implement m(List ) method.
    }
}
Because to implement an interface method, the signature must match exactly as specified by the interface (subject to the rule of covariant returns). You have to define m(List ) in CX for it to correctly implement X.

Please let us know if this addresses your doubt and if it does, we will try to make sure this point is covered in the book.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by raphaelzintec »

hi i understand now predicate

here is the video about code
https://www.youtube.com/watch?v=i0weV7KsLXc

I DIDNT KNOW THAT GENERICS WERENT CONVARIANT

now i know all OCA 8, except Localdate lol, i know only 50% of localdate because bunch of methods to memorize

btw, after i get my OCA 8 with Oracle.

how long will it takes me to learn OCP 17 from mister Hammunt? i will buy his physical book on amazon and send you proof of purchase to get pdf, because its my only way to learn trough pdfs i hate physical books

best regards

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

Re: [HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by admin »

>here is the video about code https://www.youtube.com/watch?v=i0weV7KsLXc
Ok, I though you were talking about the contents on Section 12.5.3 of the book.

>I DIDNT KNOW THAT GENERICS WERENT CONVARIANT
But this question is not about that at all. This question is about rules of implementing an interface.

>how long will it takes me to learn OCP 17 from mister Hammunt?
It is not possible to guess the time required to prepare because it depends a lot on the candidate.

>i will buy his physical book on amazon and send you proof of purchase to get pdf,
There is no physical book for OCP 17 by Hanumant Deshmukh. There is only a pdf, which you can purchase from enthuware.com/books

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by raphaelzintec »

"This question is about rules of implementing an interface."
Still not getting it sorry can you give more explanation plz?

the only thing i got is generics are not convariant that's all

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by raphaelzintec »

OCP 17 = OCA & OCP java 17 right cuz there is no more OCA 17?

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

Re: [HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by admin »

raphaelzintec wrote:
Thu Jul 18, 2024 12:38 pm
"This question is about rules of implementing an interface."
Still not getting it sorry can you give more explanation plz?

the only thing i got is generics are not convariant that's all
Please go through the detailed explanation given above. There is no mention of generics in there.

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

Re: [HD Pg 370, Sec. 12.5.3 - using-predicate-interface]

Post by admin »

raphaelzintec wrote:
Thu Jul 18, 2024 12:42 pm
OCP 17 = OCA & OCP java 17 right cuz there is no more OCA 17?
Right.


Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests