• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

avoiding down cast with polymorphism

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java it is necessary to explicitly down cast, particularly when using utilities that retrieve to Object references, for example, and code designed to benefit from polymorphism.
However, explicit down cast and code using instanceof are often responsible bugs and perpetual maintenance problems.

Does anyone know if there is an effort to eliminate the need for explicit down cast in core Java or using an extension? Any examples, extensions, or research findings that make it forever impossible to eliminate? Any comment on family polymorphism for core Java?

By the way, some fans of Scala tell me that Scala does not have this problem (that it is never necessary to explicitly down cast in Scala). Is this so? Show me. I don't know enough about Scala but it does not look that way.

Thanks in advance.


 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing to do with ORM. Moving to Java in General.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juan Rolando Prieur-Reza wrote:However, explicit down cast and code using instanceof are often responsible bugs and perpetual maintenance problems.


Why? Casting doesn't change the object type so method invocations will call the exact same methods.

Juan Rolando Prieur-Reza wrote:Does anyone know if there is an effort to eliminate the need for explicit down cast in core Java or using an extension? Any examples, extensions, or research findings that make it forever impossible to eliminate?


I don't see how this could be accomplished. Could you show some fictional code that show an example.

The closed thing I could think of is something like this:
However this only moves the cast from this class to the class of finder.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic