Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

avoiding down cast with polymorphism

 
Juan Rolando Prieur-Reza
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • 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.


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing to do with ORM. Moving to Java in General.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic