141. 142. CHAPTER 13 Generics What happens when you compile and run the following code? class Mammal {}
class Cat extends Mammal { }
List list = new ArrayList();
Answer: This code will not compile. This is because Mammal is specified
for List while Cat is specified for ArrayList. The rule is that
the type of the variable declaration must match the type that you
specify for the implementation. So, the same data type needs to
be specified in the List and ArrayList.
Will Line 1 in the following line of code compile? If not, state the reasons. class Mammal {}
class Cat extends Mammal { }
List super Mammal> mList = new ArrayList(); //
line 1
Answer: Line 1 will cause a compilation error. This is because Cat is at a
lower level of hierarchy than Mammal. Mammal is the super class.