File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes About Illegal Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Illegal Casting " Watch "About Illegal Casting " New topic
Author

About Illegal Casting

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Good day to y'all !

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 ]
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Jose,

Can you provide an example of what you have in mind? "Cast sibling objects" doesn't sound to me a good example of polymorphism (and I think the compiler does error on something like that).

Thanks.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
What do you mean by "casting sibling objects"? Could you give us an example, maybe in the form of some code?
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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? )
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

You're correct. The reason is, the Object could be a Son. The compiler doesn't know.

What it does know is that if the object is a Daughter, it can never be a Son. Well, unless it takes a gender changing operation


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Hiya Rob!

You're without a doubt the eminence when it comes to solve questions about Object Casting. Thanks for your answers Sir!

I'll take a minute to say I have noticed that you've become a SCJP for version 6.0, Congratulations....
May I ask a question?

In which topic did you fail the most?

Thanks for your time !
Your friend,
Jose
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

I think it was API. I had 80% on one, 90% on a few others, 100% on the most.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Hi Rob,

just a little question from someone still doing Java 1.4.2 (for user's convenience, so they don't have to upgrade their JVMs):

Since what version does the compiler check if type casts are sensible or not? Is that new to Java 6, or was it already included in Java 5?

Thanks,
Guido
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Java 1.4 already, possibly even sooner.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Illegal Casting