Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.82 :

Posted: Sat May 30, 2015 4:43 pm
by pushpull

Code: Select all

import java.util.*;
class Request { }
class RequestCollector{
    //1 : Insert declaration here
    
    public synchronized void addRequest(Request r){
	container.add(r);
    }
    public synchronized Request getRequestToProcess(){
	return container.poll();
    }
}

What can be inserted at //1?
In the explanation there is
(...)the use of poll() method indicates that it must be a Queue. Both LinkedList and PriorityQueue classes implement Queue interface.
So why the answer

Code: Select all

Queue container = new PriorityQueue();
is wrong?
and those two are good?

Code: Select all

Queue<Request> container = new LinkedList<Request>();
LinkedList container = new LinkedList();

Re: About Question com.enthuware.ets.scjp.v6.2.82 :

Posted: Mon Jun 01, 2015 10:38 am
by admin
In case of Queue container = new PriorityQueue(); the container is not typed to Queue<Request>. So container.poll() will not satisfy the return type of the method.

Re: About Question com.enthuware.ets.scjp.v6.2.82 :

Posted: Mon Jun 01, 2015 11:09 am
by pushpull
But the LinkedList container = new LinkedList(); isn't typed either to LinkedList<Request>, so why there is no problem here?

Re: About Question com.enthuware.ets.scjp.v6.2.82 :

Posted: Mon Jun 01, 2015 12:26 pm
by admin
You are right. This should also be a wrong option.
Fixed.

thank you for your feedback!
Paul.