About Question enthuware.ocpjp.v8.3.1858 :
Moderator: admin
-
- Posts: 2
- Joined: Tue Sep 03, 2019 10:13 am
- Contact:
About Question enthuware.ocpjp.v8.3.1858 :
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?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.3.1858 :
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:
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.
}
}
-
- Posts: 2
- Joined: Tue Sep 03, 2019 10:13 am
- Contact:
Re: About Question enthuware.ocpjp.v8.3.1858 :
Thanks admin!
Who is online
Users browsing this forum: No registered users and 9 guests