> Hello.
>
> I am trying to understand the reason for Java needing to cast the super
> class to a sub class as in the case of using the Graphics2D methods.
>
> Thanks in advance.
I have encountered this code when reading a chapter about inheritance and
polymorphism. I was just trying to understand the concept of downcasting as
shown in code line 11 and the application of it. When would I need to cast
the super class reference (pointRef) and assign it back to a sub-class
reference (circleRef).
1 public static void main( String args[] )
2 {
3 Point pointRef, p;
4 Circle circleRef, c;
5
7
6 p = new Point( 30, 50 );
7 c = new Circle( 2.7, 120, 89 );
8
9 pointRef = c;
10
11 circleRef = (Circle) pointRef;
12
13 }
Thanks for your time.
NA Jam
>> Hello.
>>
[quoted text clipped - 6 lines]
>
> Post a snippet of code!
Ryan Stewart - 31 Dec 2004 14:55 GMT
>I have encountered this code when reading a chapter about inheritance and
>polymorphism. I was just trying to understand the concept of downcasting as
>shown in code line 11 and the application of it. When would I need to cast
>the super class reference (pointRef) and assign it back to a sub-class
>reference (circleRef).
[...]
See my other post, and please do not top post.
jeffc - 31 Dec 2004 15:02 GMT
> I have encountered this code when reading a chapter about inheritance and
> polymorphism. I was just trying to understand the concept of downcasting as
[quoted text clipped - 14 lines]
> 10
> 11 circleRef = (Circle) pointRef;
You might need to do that if Circle has some additional methods that are not
defined in Point. If you were in some special code where you knew for sure
that your pointRef referred to a Circle, then you can downcast to access
Circle methods with a Point reference.