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

cast to interface

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


Even though "Unrelated" interface is no where in the hierarchy, still the code compiles fine. It will ofcourse error with ClassCastException at run time; But why does it compile? Where is the static type check the way it gets done for classes?

'appreciate any help.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler can't be sure what kind of object is referenced by the variable ac when you perform the cast to (Unrelated). For all the compiler knows, ac might be a reference to an object that is a subclass of AnyClass and that also implements Unrelated, in which case there would be no ClassCastException. Consider this example:



That code compiles and runs without any errors. Again, the compiler doesn't know at compile time what type of object ac is a reference to. It only knows that ac is an AnyClass reference which might be referencing an instance of some subclass of AnyClass, such as the class I called Sub. Because a subclass can implement the Unrelated interface even if the super class does not, the compiler assumes that you know what you're doing and that ac must be a reference to some object that implements Unrelated.
 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joseph.
That was my initial guess (after being pointed out by Marcus Green). But should not the compiler see that theres no such class hierarchy exists that can make it possible?

Well the outcome is: You can cast any class to any interface!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasanta Chinara:

Well the outcome is: You can cast any class to any interface!



No, actually. Not if the type of the reference is a final class, for example.
 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest for point that out!
Thanks all for taking time to reply to my query.
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasanta Chinara:
But should not the compiler see that theres no such class hierarchy exists that can make it possible?



No, the compiler cannot and should not make any assumptions about the existence of a class hierarchy. There are many classes that are not part of the standard Java libraries, including the classes you make yourself. If the compiler assumed that the only classes in existence were the ones in the standard library, then you and I would not be able to compile or use any new classes.
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic