Page 1 of 1

About Question enthuware.ocpjp.v7.2.1292 :

Posted: Sun Sep 06, 2015 12:26 am
by boyonsea
Why cant "Queue container = new PriorityQueue();" be used?

Re: About Question enthuware.ocpjp.v7.2.1292 :

Posted: Sun Sep 06, 2015 1:09 am
by admin
The first sentence of the explanation explains exactly what you are asking. I am quoting it here for your convenience,
Since the return type of getRequestToProcess() method is Request, and the return statement does not have a cast, it means that container must be a typed generic collection that returns Request objects only.

Re: About Question enthuware.ocpjp.v7.2.1292 :

Posted: Wed Dec 09, 2015 7:12 am
by unvector
If there would be such answer:
Queue<Request> container = new PriorityQueue<Request>();
Would it be correct? it would compile, but PriorityQueue would throw exception while adding an element to nonempty queue.

Re: About Question enthuware.ocpjp.v7.2.1292 :

Posted: Wed Dec 09, 2015 8:46 pm
by admin
Yes, it would have been valid. Yes, it is possible for an exception to be thrown if the code is not used correctly but that is irrelevant for the question.

HTH,
Paul.