Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Trying to Understand Super class and sub class relationship
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.




Report this thread to moderator Post Follow-up to this message
Old Post
N.A. Jam
12-31-04 08:58 PM


Re: Trying to Understand Super class and sub class relationship
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!

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew McDonagh
12-31-04 08:58 PM


Re: Trying to Understand Super class and sub class relationship
"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?



Report this thread to moderator Post Follow-up to this message
Old Post
Ryan Stewart
12-31-04 08:59 PM


Re: Trying to Understand Super class and sub class relationship
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!



Report this thread to moderator Post Follow-up to this message
Old Post
N.A. Jam
12-31-04 08:59 PM


Re: Trying to Understand Super class and sub class relationship
"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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ryan Stewart
12-31-04 08:59 PM


Re: Trying to Understand Super class and sub class relationship
"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.



Report this thread to moderator Post Follow-up to this message
Old Post
jeffc
12-31-04 08:59 PM


Re: Trying to Understand Super class and sub class relationship
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!);

Report this thread to moderator Post Follow-up to this message
Old Post
Paul van Rossem
12-31-04 08:59 PM


Re: Trying to Understand Super class and sub class relationship
"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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ryan Stewart
12-31-04 08:59 PM


Re: Trying to Understand Super class and sub class relationship
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Paul van Rossem
12-31-04 08:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:41 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.