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

Question #4 from Doug's book

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This would fail to compile:

[ August 11, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic