Java Magazine, Sept/Oct 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 81 fix this eral simplification of the resulting code around the get operation any exception that arises in the call method is wrapped in an ExecutionException before being thrown out of the get method The ExecutionException is also a checked exception Therefore option E is also correct Given that options D and E are correct the If the compiler cant prove safety the generics system must refuse to compile the code However a workaround is provided by the generics syntax same logic shows that option A must be incorrect Further because the requirement already exists for handling all exceptions that could arise from the call method option B is incorrect The call to get requires exception handling but this has nothing to do with whether the call method is modified The infrastructure of the get method must handle any exceptions from any Callable regardless of whether they actually exist in a particular implementation Question 4 The correct answers are options A and C This question investigates the relationship between generalization and collections in general Its tempting to think that a List Child might be a valid substitute for a List Parent and that option B might be valid However even though this code does nothing that would actually cause any problems at runtime it would not compile The reason is that in general such a substitution is not safe Imagine if the doBatch method attempted to add an item to the List it receives Its proper to add a Parent object to a List Parent however its not safe to do that with a List Child For this reason the compiler must not permit the pairing described in option B because it would create the possibility of runtime errors Therefore option B is incorrect Its probably fair however to think that much of the time when you pass a collection of something into a method you want to perform some iterative processing on each member of the collection and you wont be adding items to the collection If you could do that safely the ability to pass many Child objects into a method that expects the Parent type would be quite useful In fact if the many objects comprise an array the operation is reasonably safe This is
You must have JavaScript enabled to view digital editions.