• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

child object and parent object... assignable?

 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valid: b=a
Invalid: a=b
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sangita Mishra
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No in certain cases (when assigning child object to Parent reference), it gives runtime expetion. see example below:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic