Multiple choice technology web technology

Given: 3. import java.util.*; 4. class Business { } 5. class Hotel extends Business { } 6. class Inn extends Hotel { } 7. public class Travel { 8. ArrayList go() { 9. // insert code here 10. } 11. } Which, inserted independently at line 9, will compile? (Choose all that apply.)

  1. return new ArrayList<Inn>();

  2. return new ArrayList<Hotel>();

  3. return new ArrayList<Object>();

  4. return new ArrayList<Business>();

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In Java, generic types must match exactly. The method return type is ArrayList, so the returned object must be of type ArrayList. Subtyping of the type arguments does not apply to the generic class itself (i.e., ArrayList is not a subtype of ArrayList).