Originally posted by Veena Point:
Alton,
Can I say that , "since Collection extends Object & HashMap extends AbstractMap which inturn extends Object " ,they all lie in the inheritance hirarchy?
Hi Veena,
I don't think that is the right explanation. The code below would not compile even though A extends Object and B extends Object.
For instanceof to compile properly, the reference(operand1) and the classs type(operand2) must be in the same lineage. And in the example above, A and B are not.
But in the case of an interface, it is different. The compiler has to allow casting of an interface to a class and vice-versa, even though no obvious relationship exists between the two, because down the line, the subclass may still implement that interface.
If you look at my first sample code, even though class A and interface I has no relationship, it is still possible to do this although it will turn out as an invalid casting.
The compiler has no idea if the subclass of A will implement the interface I, as in this case:
If the compile would not allow it, then this will fail, although the casting of a to I, and i to A is valid.
[ September 14, 2003: Message edited by: Alton Hernandez ]