| Author |
Trying to Understand Super class and sub class relationship
|
|
| N.A. Jam 2004-12-31, 3:58 pm |
| 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.
| |
| Andrew McDonagh 2004-12-31, 3:58 pm |
| N.A. Jam wrote:
> 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.
>
>
>
Hi I'm trying to understand your question...
Post a snippet of code!
| |
| Ryan Stewart 2004-12-31, 3:59 pm |
| "N.A. Jam" <paulipino2000-a@yahoo.ca> wrote in message
news:FPCdneIPT_ejz0jcRVn-tQ@rogers.com...
> 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.
>
Say I have this somewhere:
Object o = "Hello world";
Now say I want to find the length of the String referenced by o. How do I do
it?
| |
| N.A. Jam 2004-12-31, 3:59 pm |
| 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
"Andrew McDonagh" <news@andrewcdonagh.f2s.com> wrote in message
news:cr3k1p$vkj$1@news.freedom2surf.net...
> N.A. Jam wrote:
>
> Hi I'm trying to understand your question...
>
> Post a snippet of code!
| |
| Ryan Stewart 2004-12-31, 3:59 pm |
| "N.A. Jam" <paulipino2000-a@yahoo.ca> wrote in message
news:Sbydnb2yO-dmxEjcRVn-2Q@rogers.com...
>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.
| |
|
|
"N.A. Jam" <paulipino2000-a@yahoo.ca> wrote in message
news:Sbydnb2yO-dmxEjcRVn-2Q@rogers.com...
> 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;
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.
| |
| Paul van Rossem 2004-12-31, 3:59 pm |
| On 31-12-2004 14:32, Ryan Stewart wrote:
> "N.A. Jam" <paulipino2000-a@yahoo.ca> wrote in message
> news:FPCdneIPT_ejz0jcRVn-tQ@rogers.com...
>
>
> Say I have this somewhere:
> Object o = "Hello world";
>
> Now say I want to find the length of the String referenced by o. How do I do
> it?
>
>
if (o instanceof String)
int length = ((String)o).length();
else
throw new Exception("Not a String!);
| |
| Ryan Stewart 2004-12-31, 3:59 pm |
| "Paul van Rossem" <paul@timeware.nl> wrote in message
news:41d58293$0$6203$e4fe514c@news.xs4all.nl...
> On 31-12-2004 14:32, Ryan Stewart wrote:
> if (o instanceof String)
> int length = ((String)o).length();
> else
> throw new Exception("Not a String!);
>
That was for the OP to figure out.
| |
| Paul van Rossem 2004-12-31, 3:59 pm |
| On 31-12-2004 19:46, Ryan Stewart wrote:
> "Paul van Rossem" <paul@timeware.nl> wrote in message
> news:41d58293$0$6203$e4fe514c@news.xs4all.nl...
>
>
> That was for the OP to figure out.
>
>
OK, sorry, should have read the whole thread more carefully...
Paul.
|
|
|
|