aspose file tools*
The moose likes Java in General and the fly likes avoiding down cast with polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "avoiding down cast with polymorphism" Watch "avoiding down cast with polymorphism" New topic
Author

avoiding down cast with polymorphism

Juan Rolando Prieur-Reza
Ranch Hand

Joined: Jun 20, 2003
Posts: 236
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.



Juan Rolando Prieur-Reza, M.S., LSSBB, SCEA, SCBCD, SCWCD, SCJP/1.6, IBM OOAD, SCSA
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Nothing to do with ORM. Moving to Java in General.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: avoiding down cast with polymorphism