• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Strange casting...

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to everyBody,

I noticed that a java.util.Map is castable to java.io.Serializable.
But java.util.Map and java.io.Serializable are not in relation.
Why can I cast a class to another one even if they are not in any relation?
It is neither upcasting nor downcasting...

Thanks!
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ignore, that I'm not using any generic type parameters.

Consider this code, and see if you can answer your question now:

[ October 04, 2007: Message edited by: John Stone ]
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason its always allowed that you can cast to inteface type, because even if the class doesnt implement that inteface, a sub class might, with exception if the class is final then cast can fail.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
thanks for your response.

Well, in general, I have two interfaces I1, I2, that are NOT in relation to each other.
Then, there is a class C1 that implements both I1 and I2 and a class C2 that implements only I1.
It seems to be legal to cast a reference for I1 to the type I2.
I mean:


(In our case I1=java.util.Map, I2=java.io.Serializable, C1=java.util.HashMap, C2=java.util.jar.Attributes)

But this situation, in general, is NOT valid. In fact, if you cast a java.util.Map to a java.util.List, it is not legal. But the relationship between java.util.Map and java.util.List is the same as java.util.Map and java.io.Serializable, except for the fact that today nobody has yet created a class that both implements java.util.Map and java.util.List.
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check Ahmed's post. He wrote why it is working that way. You need to distinguish between: "cast is allowed at compile-time" / "cast will fail at run-time".
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok,


The reason its always allowed that you can cast to inteface type


So, why can't I cast a java.util.Map (interface) to a java.util.List(interface, too)?
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is allowed at compile-time:

but will fail at runtime:
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So, why can't I cast a java.util.Map (interface) to a java.util.List(interface, too)?


How are you doing it? can you us some code..
Normally you will have to cast interface implementer to interface type.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for your response!

Here a code snippet that creates to me a lot of confusion...

 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No Compiler error nor Runtime error
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
here the exact file I compiled:


I get the error:
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh!

I had not read the message!

Thanks you all!
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic