Folks,
This question comes out of the K&B book, self
test question #2 on Pg.195.
The question is as follows:
2. Given the following.
1. import java.awt.*;
2. class Ticker extends Component {
3. public static void main(
String[] args) {
4. Ticker t = new ticker();
5.
6. }
7. }
which two of the following statements, inserted independnently, could legally be inserted into line 5 of this code?(choose two)
A. boolean test = (Component instanceof t);
B. boolean test = (t instanceof Ticker);
C. boolean test = t.instanceof (Ticker);
D. boolean test = (t instanceof Component);
E. boolean test = t.instanceof (Object);
F. boolean test = (t instanceof String);
The answer is B and D, but why not F? I get a compile time error when inserting the expression. String is a subclass of Object so why can't I use the instanceof operator? Although I know they are entirely different objects, isn't that the whole point of instanceof. It should tell me if they are the same type of object so then I can make an implicit cast. Like when you overide the equals method. Any clarrification is appreciated.
Thanks,
Andreas
[ October 06, 2005: Message edited by: Andreas Sandberg ]