Which, inserted at line 9, will compile? (Choose all that apply.)
A. x2.do2();
B. (Y)x2.do2();
C. ((Y)x2).do2();
D. None of the above statements will compile.
Answer:
C is correct. Before you can invoke Y’s do2 method you have to cast x2 to be of type
Y. Statement B looks like a proper cast but without the second set of parentheses, the
compiler thinks it’s an incomplete statement.
A, B and D are incorrect based on the preceding.
(Objective 5.2)
==
class X { void do1() { } } class Y extends X { void do2() { } } class Chrome { public static void main(String [] args) { X x1 = new X(); X x2 = new Y(); Y y1 = new Y(); // insert code here } }
No comments:
Post a Comment