Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

Trying to Understand Super class and sub class relationship

Thread view: 
N.A. Jam - 31 Dec 2004 13:20 GMT
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 - 31 Dec 2004 13:26 GMT
> 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!
N.A. Jam - 31 Dec 2004 13:53 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).

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.
Ryan Stewart - 31 Dec 2004 13:32 GMT
> 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?
Paul van Rossem - 31 Dec 2004 16:47 GMT
>>Hello.
>>
[quoted text clipped - 8 lines]
> 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 - 31 Dec 2004 18:46 GMT
>>>Hello.
>>>
[quoted text clipped - 12 lines]
> else
>   throw new Exception("Not a String!);

That was for the OP to figure out.
Paul van Rossem - 31 Dec 2004 18:52 GMT
>>>>Hello.
>>>>
[quoted text clipped - 15 lines]
>
> That was for the OP to figure out.

OK, sorry, should have read the whole thread more carefully...
Paul.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.