File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question #4 from Doug's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question #4 from Doug Watch "Question #4 from Doug New topic
Author

Question #4 from Doug's book

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Casting between interfaces is almost never a compile problem. For example:

This compiles (and runs) without a problem.
There is only one situation that will prevent you from casting from one interface type to another. What is it?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
when we try to cast from final class Object which doesn't implement the interface
[ August 11, 2003: Message edited by: Praveen Kumar Mathaley ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Although that is true, that is not the question. I am casting from one interface to another so neither can be final. Java almost always allows this without complaint from the compiler because there is no way for it to know if the underlying class (the compiler has no idea what that class might be) implements both interfaces or not. With one exception.
Hint: There is one case where the compiler knows that the underlying class can't possibly implement the two interfaces involved in the casting. Remember that the compiler has no iea what the underlying class is because we are casting one interface to another. So what is it about the interfaces that make them not compatible for casting?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
This would fail to compile:

[ August 11, 2003: Message edited by: Alton Hernandez ]
Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
I buzzed out to the JLS looking for the answer. The only thing I found is the example that Alton Hernandez is showing. Based on the hint that you gave this must not be what you are looking for. I am afraid you have forgotten about the question, what is the answer?
Selim Hendrickson
Greenhorn

Joined: Aug 14, 2003
Posts: 9
I'm not very clear about the question after the rejected answers but I think you can't cast from one interface to another if both interfaces have methods with same signatures but different return types. :roll:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Based on the hint that you gave this must not be what you are looking for.
I think Tom's hint is completely consistent with Alton's answer (also Selim's) - which as far as I know is the only answer to Tom's question.


"I'm not back." - Bill Harding, Twister
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I'm sorry, were you waiting for me? Alton was correct. If two interfaces have identical signatures with different return values they are incompatible. That means that no class can implement both of them. Therefore casting between them without a ClassCastException would be impossible so the compiler doesn't permit the cast.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question #4 from Doug's book