Saturday, August 21, 2010

Generics: Test.java

Courtesy of Sun Web Learning Center

3. import java.util.*;
4. class Car { }
5. class Honda extends Car { }
6. public class Test {
7.   public static void main (String[] args) {
8.     List<Car> cars = new ArrayList<Car>();
9.     List<Honda> cars2 = new ArrayList<Honda>();
10.     List<Object> cars3 = new ArrayList<Object>();
11.     takeCars(cars);
12.     takeCars(cars2);
13.     takeCars(cars3);
14.   }
15.   //  insert code here
16. }

Which two, inserted independently at line 15, allow the file to compile?


A     public static void takeCars(List<?> list) { }
B     public static void takeCars(List<Object> list) { }
C     public static void takeCars(List<? extends Car> list) { }
D     public static void takeCars(List<T extends Object> list) { }
E     public static void takeCars(List<? extends Object> list) { }

A and E are the correct syntax to handle all three types of Lists in the takeCars argument.

No comments:

Post a Comment