Sunday 17 March 2013

SCJP - 17 - Given Example




Which statement(s), inserted at line 6, will compile? (Choose all that apply.)
A. Flower getType() { return this; }
B. String getType() { return "this"; }
C. Plant getType() { return this; }
D. Tulip getType() { return new Tulip(); }

Answer:

A, C, and D are correct. A and D are examples of co-variant returns, i.e., Flower and
Tulip are both subtypes of Plant.

B is incorrect, String is not a subtype of Plant.
(Objective 1.5)
==
class Plant {
String getName() { return "plant"; }
Plant getType() { return this; }
}
class Flower extends Plant {
// insert code here
}
class Tulip extends Flower { }


No comments:

Post a Comment