P
Peter Duniho
My apologies if this is a silly question in the context of Java. I'm very
new to the language, though I've got lots of experience in others
(including C#, which has borrowed a lot from Java) and programming
generally.
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" (for
example, in C# you can write explicit type conversions that allow
arbitrary code to execute to convert from one type to another).
The two particular examples are Graphics and Graphics2D, and Rectangle2D
and Rectangle2D.Double.
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?
* Is there a type conversion that will ensure it always will?
* Or will the code fail if it's run in an environment where a plain
Graphics instance is passed?
The second example comes up in the context of using an Area object. I
want to be able to get the bounding rectangle from the Area object and use
the exact location and size of the rectangle to do some geometric
contraining code. But Area.getBounds() returns a Rectangle2D, which
doesn't expose the fields that provide that information (they're in the
sub-classes).
The Rectangle class (base class for Rectangle2D) does have "get" methods
for these values (getX(), getY(), getWidth(), getHeight()) and I can use
those. But for grins I tried casting the Rectangle2D to a
Rectangle2D.Double and it worked fine. Again, I'm curious of pretty much
the same questions I have for the Graphics class. Is this something I can
either count on, or force to always happen? Or did I just luck out?
Thanks,
Pete
new to the language, though I've got lots of experience in others
(including C#, which has borrowed a lot from Java) and programming
generally.
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" (for
example, in C# you can write explicit type conversions that allow
arbitrary code to execute to convert from one type to another).
The two particular examples are Graphics and Graphics2D, and Rectangle2D
and Rectangle2D.Double.
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?
* Is there a type conversion that will ensure it always will?
* Or will the code fail if it's run in an environment where a plain
Graphics instance is passed?
The second example comes up in the context of using an Area object. I
want to be able to get the bounding rectangle from the Area object and use
the exact location and size of the rectangle to do some geometric
contraining code. But Area.getBounds() returns a Rectangle2D, which
doesn't expose the fields that provide that information (they're in the
sub-classes).
The Rectangle class (base class for Rectangle2D) does have "get" methods
for these values (getX(), getY(), getWidth(), getHeight()) and I can use
those. But for grins I tried casting the Rectangle2D to a
Rectangle2D.Double and it worked fine. Again, I'm curious of pretty much
the same questions I have for the Graphics class. Is this something I can
either count on, or force to always happen? Or did I just luck out?
Thanks,
Pete