Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cast to interface

 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic