164k views
2 votes
Let Dog be a subclass of Animal, and suppose Animal has a method called speak() that is overridden in the Dog class. Consider the following code. [2 points] Animal spot = new Dog();

spot. speak(); Which of the following is true? a) This code will result in a compile-time error. b) This code will result in a run-time error. c) The speak method defined in the Animal class will be called. d) The speak method defined in the Dog class will be called. e) The speak method will not be called at all.

User DeanOC
by
8.8k points

1 Answer

2 votes

Answer:

d) The speak method defined in the Dog class will be called.

Step-by-step explanation:

In this code, a new instance of the Dog class is created and assigned to a variable of type Animal. When the method speak() is called on this instance using the variable spot, the overridden version of speak() in the Dog class will be called, since method calls are determined at runtime based on the actual object type, not the variable type. Therefore, option d) is the correct answer.

User Eric Wiener
by
8.7k points