This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes cast to interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "cast to interface" Watch "cast to interface" New topic
Author

cast to interface

Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30


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

Joined: Apr 10, 2005
Posts: 195
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.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
Thanks Ernest for point that out!
Thanks all for taking time to reply to my query.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cast to interface
 
Similar Threads
Question about overriding
Enum Problem
Generic Colletion <? super String>
Possible error in K&B book (resolved - no error)
An urgent doubt