• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question #4 from Doug's book

 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Praveen Kumar Mathaley
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would fail to compile:

[ August 11, 2003: Message edited by: Alton Hernandez ]
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic