Page 1 of 1
About Question enthuware.ocpjp.v8.3.1858 :
Posted: Tue Sep 03, 2019 10:16 am
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?
Re: About Question enthuware.ocpjp.v8.3.1858 :
Posted: Tue Sep 03, 2019 10:39 am
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.
}
}
Re: About Question enthuware.ocpjp.v8.3.1858 :
Posted: Tue Sep 03, 2019 12:09 pm
by rvichith
Thanks admin!