Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Illegal Casting

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it was API. I had 80% on one, 90% on a few others, 100% on the most.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.4 already, possibly even sooner.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic