Given the class definitions below, which value, when inserted into the blank line, does not allow the class to compile?
public class Animal {} public class Dog extends Animal {} public class Wolf extends Animal {} public final class Husky extends Dog {} public class Main { Animal animal; //from w w w. j a va2 s .c o m public final void setAnimal(Dog animal) { this.animal = animal; } public static void main(String[] furryFriends) { new Main().setAnimal( ___ ); } }
Husky()
Dog()
Wolf()
C.
The setAnimal()
method requires an object that is Dog or a subclass of Dog.
Since Husky extends Dog, Options A and B both allow the code to compile.
Option D is also valid because a null value does not have a type and can be assigned to any reference variable.
Option C is the only value that prevents the code from compiling because Wolf is not a subclass of Dog.
Even though Wolf can be assigned to the instance Animal variable, the setter requires a compatible parameter.