Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

When can I cast one object to another object?

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not familiar with casting. Can anyone please explain to me how and when to use casting?

Thanks for your help!
Mindy
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To cast, the types must be related and/or compatible. You can cast a parent object to a child object ( ChildObj = (ChildClass)ParentObj ; ) because they are related. You can cast between primitives ( float f = 1.5; int i = (int)f ; ) when they are compatible.
You cast when you have a type that you need to be of another related/compatible type.
Hope this helps.
Made following edit based on Steven's comment
( ChildObj = (ChildClass)ParentObj ; ) when they are related.
to
( ChildObj = (ChildClass)ParentObj ; ) because they are related.
Happy?
[This message has been edited by Richard Boren (edited June 14, 2001).]
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can cast a parent object to a child object ( ChildObj = (ChildClass)ParentObj ; ) when they are related.
By "related" , I'm pretty sure you mean the base class / sub class relationship. When is a sub class NOT related to it's base class?? ...or am I just reading things into it.
SteveII
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I see...I'm just now starting to study mock exams, and for a minute there, I thought I was missing something terribly obvious. I'm trying to confront every little thing that gives me doubt so I don't get off track.
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys! I got it now!
Hey Steven, may I ask you that what book did you study to prepare for the JSCP exam. I am planning to take it in the next few months, and wodering if you can give me some idea of how to get started.
I will be appreciate for your reply!
Good Luck!
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand the difference between TYPE of an object reference and the CLASS of the object before you play around with casting.
I wrote a rather lengthy explanation here http://www.javaranch.com/ubb/Forum33/HTML/002413.html
a while ago.
Hope this helps.
Jyotsna
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mindy,
It has taken me an awfully long time before I even felt comfortable enough to studying for certification. I'm still not all that great, but after the last frustration break, it seems that things are becoming more obvious. So... I've finally started to study mock exams, which is really what I should have done from the beginning. Starting with Rules Roundup, I've been coding demonstrations to the ones that I get wrong, or want to understand more. Any question or doubt, I post to the certification forums.
I've downloaded several online tutorials (Thinking in Java, Richard Baldwin's, Sun's "Brewing Java"?). I like Ivor Horton's "Beginning Java2". It's a doorstop, but it's good.
[This message has been edited by Steven YaegerII (edited June 16, 2001).]
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jyotsna Clarkin:
... I wrote a rather lengthy explanation here http://www.javaranch.com/ubb/Forum33/HTML/002413.html
a while ago. ...
Jyotsna


Excellent explanation Jyotsna.
reply
    Bookmark Topic Watch Topic
  • New Topic