About Question enthuware.ocpjp.v8.3.1858 :

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

Moderator: admin

Post Reply
rvichith
Posts: 2
Joined: Tue Sep 03, 2019 10:13 am
Contact:

About Question enthuware.ocpjp.v8.3.1858 :

Post by rvichith »

A Consumer<T> has the following method signature, void accept(T t). Whereas a List.add has the following, boolean add(E e). The add method returns a boolean whereas the accept method does not. Then how can we use List.add in place of Consumer.accept?

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

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

Post by admin »

Remember that method signature includes only method name and parameter type list. It does not include return type. So, the fact that the add method returns a boolean is irrelevant.
That is why a Consumer instance can be easily implemented using list.add. Something like this:

Code: Select all

class C implements Consumer{
   void accept(T t){
       list.add(t); //the value returned by this call is ignored.
   } 
}

rvichith
Posts: 2
Joined: Tue Sep 03, 2019 10:13 am
Contact:

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

Post by rvichith »

Thanks admin!

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests