I've been thinking lately about polymorphism, and I came to an interesting thought.
Why is it that the compiler doesn't alert the programmer of really Obvious Illegal Cast(s) ?
For example, let's talk about the Scenario of tryin' to cast Sibling Objects. -That 'obviously' can never happen, however the compiler never tells you it's wrong CORRECTION: IT DOES TELL YOU IT'S WRONG, therefore a programmer will only be able to know what he did wrong at run-time when a ClassCastException shows up...
Why can't the compiler know when Illegal/Impossible casts are going to be attempted?
Thank you very much for your time, Jose [ May 02, 2008: Message edited by: Jose Campana ]
What do you mean by "casting sibling objects"? Could you give us an example, maybe in the form of some code?
Joined: May 28, 2007
Hello there true believers !
Casting sibling Objects Has nothing to do with polymorphism (as far as I know), and it means Casting between Objects that share a common Parent or Super class, but aren't in line in the same inheritance tree, for example...
In the preceding code Son and Daughter are Siblings.
But you were right about one thing, When casting Siblings the compiler Does give an error.
But not when you try to DownCast an Object to a SubClass. ( Am I correct? )
I think that's always been part of Java. At least, the Java Language Specification has had rules about this since it first came out; see here. It's possible that some JDK versions might have had some bugs that prevented this from working correctly; I don't really know. But I don't remember any problems in this area. I first used JDK 1.1, though I didn't understand its details very well at the time. [ May 03, 2008: Message edited by: Jim Yingst ]