Which, inserted at line 9, will compile? (Choose all that apply.)
A. Beagle b2 = (Beagle) dog1;
B. Beagle b3 = (Beagle) dog2;
C. Beagle b4 = dog2;
D. None of the above statements will compile
Answer:
A and B are correct. However, at runtime, A will throw a ClassCastException because
dog1 refers to a Dog object, which can’t necessarily do Beagle stuff.
C and D are incorrect based on the preceding.
(Objective 5.2).
==
class Dog { } class Beagle extends Dog { } class Kennel { public static void main(String [] arfs) { Beagle b1 = new Beagle(); Dog dog1 = new Dog(); Dog dog2 = b1; // insert code here } }
No comments:
Post a Comment