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

Question about casting to String

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can you please tell me why Java considers A compatible with Face, but string is not?? Sorry if this question was already discussed in the past, I cannot find it.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Danilou wrote:
Can you please tell me why Java considers A compatible with Face, but string is not?? Sorry if this question was already discussed in the past, I cannot find it.


JLS about legal casting (5.1.6 Narrowing Reference Conversions):

From any interface type J to any non-parameterized class type C that is not final.


Because the String class is final, whereas A is NOT final. So, for the compiler, it could be possible that the class A is subclassed by an class implementing the Face interface
and extending the A class. That way the cast would be fine and even the runtime won't complain.

 
Alexander Danilou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bob,
I will try to go thru JLS next time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic