Re: Type conversion for reference types?
Peter Duniho wrote:
I am wondering what facility for type conversion of reference types, if
any, exists in Java. And more specifically, if none exists, how do I
reliably deal with situations where I want a specific sub-class but am
only given some base class. I mean, is there anything unique to Java
that provides for this, or is it really just as simple as "casting only
works if the actual instance type is compatible with the destination type"
Downcasts are legal, upcasts are unnecessary.
You can always cast from a base type, e.g., Number, down to the actual type of
the object at run-time, or anything in between:
Number someNumber = getItSomehow();
Long lval = (Long) someNumber;
Of course, if someNumber is actually a Double, you'll raise a ClassCastException.
Upcasting is a "widening conversion" and doesn't require explicit syntax:
Number num = Long.valueOf( 1L );
(for example, in C# you can write explicit type conversions that
allow arbitrary code to execute to convert from one type to another).
Not so, Java. You can in some situations write a constructor or factory
method that generates an equivalent instance in a different class:
Number num = Long.valueOf( "1" );
You need to call this method everywhere it matters.
String momsAge = request.getParameter( "momsAge" );
Person mom = new Mom();
mom.setAge( Long.valueOf( momsAge ));
You never know. Cryogenics, advances in stem cells, genetics, they'll cure
all viral diseases - better to give enough range.
With respect to Graphics/Graphics2D, I've seen lots of examples of AWT
code where in a paint() method, the Graphics instance passed in is cast
to Graphics2D. I've done this myself in my own code and it appears to
work fine. But:
* How can I be guaranteed that the cast will succeed?
Someone up in Valhalla said, "Trust us!"
Don't worry, if they ever change it then your program will blow up with a
ClassCastException, so you'll know.
* Is there a type conversion that will ensure it always will?
It's called, "The actual object always (coincidentally) happens to be a
Graphics2D." In other words, no. But you're talking about the Swing API,
which happens to use only Graphics2D objects, so you're good.
Here's one for people who complain about Java's type safety. You are asking
for more type safety - a more restrictive type in the method signature. As
well you should - it is very much a hack that type safety is circumvented
here. I believe it has to do with needing to override AWT methods, which do
not guarantee to have Graphics2D arguments, and thus not being able to change
the signature. So the Swing hack is not to promise you anything, but give you
(nudge, nudge, wink, wink) a Graphics2D on the sly.
* Or will the code fail if it's run in an environment where a plain
Graphics instance is passed?
These are Swing methods. Swing methods will do what they're documented to do.
Cast away with impunity.
You could, I suppose, write a custom component that somehow subverts the
promise. Don't do that.
I leave the other questions to those more expert than I.
--
Lew