File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes child object and parent object... assignable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "child object and parent object... assignable?" Watch "child object and parent object... assignable?" New topic
Author

child object and parent object... assignable?

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1558
Let us say, class A that extends class B and their respective objects are "a" and "b"...

A= child class
B= parent class
Now, which one is valid to assign.

a=b; or
b=a;
or both?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Valid: b=a
Invalid: a=b
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
A= child class
B= parent class

then
b=a; is valid implicitly

But you can do:
a=(A)b; explicitly. This may result in runtime exception.
(ClassCastException).For that u can use try catch block.


bye

Shubha.


Shubhada
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Originally posted by Shubha AN:
A= child class
B= parent class

then
b=a; is valid implicitly

But you can do:
a=(A)b; explicitly. This may result in runtime exception.


I think it alway result in runtime exception. Can someone judge me this please?


SCJA, SCJP5.0, SCBCD, SCWCD
Sangita Mishra
Greenhorn

Joined: Jun 15, 2005
Posts: 22
No in certain cases (when assigning child object to Parent reference), it gives runtime expetion. see example below:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: child object and parent object... assignable?