This is a piece of code written by self for experimentation purposes.
If I have the following code, my output is Superclass.
Whereas, if I change the order of mmy if-else clause like this, the output is Subclass.
And if I do not have the "else" keyword and just two if-s, both Superclass and Subclass are printed? Does it mean that the reference "r" is instance of both the sub and super classes? I can understand that polymorphism is into play here, with all the overriding rules of choosing the function. But, any special reasons as to why this behaviour occurs?
instanceof operator checks for IS-A relationship. That means objects of all subclasses will pass the instanceof test for superclass name.
Here in this code: r is an instance of Sub class which extends Super. Thats the reason, both expressions r instanceof Sub and r instanceof Super will return true and the print stmt will get executed. And else condition will be skipped.
No question is silly.. Now you have a better understanding.. Dont hesitate to post silly questions, if they bother you.. Posting a silly question is better than not posting it at all and being in doubt.. [ September 29, 2008: Message edited by: Paul Somnath ]