Given the following class declarations:
public abstract class Animal
public interface Hunter
public class Cat extends Animal implements Hunter
public class Tiger extends Cat
public interface Hunter
public class Cat extends Animal implements Hunter
public class Tiger extends Cat
Which answer fails to compile?
A.
ArrayList<Animal> myList = new ArrayList<>();
myList.add(new Tiger());
B.
ArrayList<Hunter> myList = new ArrayList<>();
myList.add(new Cat());
C.
ArrayList<Hunter> myList = new ArrayList<>();
myList.add(new Tiger());
D.
ArrayList<Tiger> myList = new ArrayList<>();
myList.add(new Cat());
E.
ArrayList<Animal> myList = new ArrayList<>();
myList.add(new Cat());
题解
选项 A,Tiger 是 Animal(Tiger 继承 Cat,Cat 继承 Animal),所以这没有问题。
选项 B,Cat 是 Hunter(Cat 实作 Hunter),所以这没有问题。
选项 C,Tiger 是 Hunter(Tiger 继承 Cat,Cat 实作 Hunter),所以这没有问题。
选项 D,Cat 不是 Tiger,会编译错误。
选项 E,Cat 是 Animal(Cat 继承 Animal),所以这没有问题。
