• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

cast null to interface and class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why only line1 compile ok? Is interface different from class when handling casting? how?
I was told that null can be cast to any classes, but it seems wrong.
[ April 12, 2004: Message edited by: Wei Lien Chun ]
Edited by Corey McGlone: Added CODE Tags
[ April 12, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the fact that these variables refer to null doesn't make any difference whatsoever. Let's go through the various lines and determine why you're getting errors:

This line compiles fine. The reason for that is that, even though A does not implement I1, some subclass of A might, which might make this a legal cast. Therefore, this line will compile correctly. Check out my blog entry for more details on this case.

This case is not allowed because a2, which is of type A does not implement I1. Therefore, the compiler knows that this assignment makes no sense and flags it with an error.

This is also illegal because a3, which is of type A, can't possibly refer to an object of type B as A and B are not related. You can also look at my blog entry for more information, if you'd like.

Again, this doesn't work because A and B are not related. This is essentially the same case as the one above.
I hope that helps,
Corey
 
Wei Lien Chun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow!!Corey
Thanks a lot to all of you!
 
permaculture is giving a gift to your future self. After reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic