Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parent and child class relationship.

 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Parent { }
class DerivedOne extends Parent { }
class DerivedTwo extends Parent { }
Parent p = new Parent();
DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
1.Legal at compile,fails at runtime.
p=d1;
2.Illegal at compile and runtime.
d1 =d2;
d1=(Derivedone)d2;
d1=(Derivedone)p;
3.Legal at compile time and runtime
p=(Parent)d1;

Am i right ??Pl.clarify.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by geetha nagarajan:
1.Legal at compile,fails at runtime.
p=d1;
2.Illegal at compile and runtime.
d1 =d2;
d1=(Derivedone)d2;
d1=(Derivedone)p;
3.Legal at compile time and runtime
p=(Parent)d1;

Did you try plopping it in a file and letting the compiler tell you what was legal or not??
1.Legal at compile,fails at runtime.
p=d1;

WRONG -- it's both legal at compile time and at runtime, because DerivedOne is-a Parent.
#3 is correct for the same reason, the explicit cast to type Parent is not necessary.
2.Illegal at compile and runtime.
d1 =d2;
d1=(DerivedOne)d2;
d1=(DerivedOne)p;

CORRECT -- because the above do not have a is-a relationship
[ June 03, 2002: Message edited by: Jessica Sant ]
 
John Loney
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p = d1;
Legal and compile and runtime. This is because
automatic conversions are legal going up the hierarchy. What helps me to remember how this works is to think that since d1 is an object whose class is a subclass of Parent, d1 has all information needed to create an instance of Parent.
Where as if you go down the hierarchy, Parent does not have information to create in instance of
DerivedOne, so you need an explicit cast.
 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
By,the way i took this mock exam,
http://www.geocities.com/SiliconValley/Orchard/9362/java/javacert/newcert1-10.html
After i answered them,wanted to check if they were right or not,but there was no answers on that site.
So,posted it here,to confirm my answers.
 
Brett Swift
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I've been studying too hard and confused myself on this issue, but when will you need an explicit cast on objects? If an object can be cast up implicitly (super = sub ;) but it is illegal to cast down or latterally explicity (sub = (sub)super; , or sub1 =(sub1)sub ;) , then when can you ever make use of an explicit cast with objects?
Example:
This will compile but won't run.
class Base(){}
class Sub()extends Base{}
class SubOne
{
public static void main(String args[])
{
Base b = new Base();
Sub s= (Sub)b;
}
}
[ Disabled Smilies ]
[ June 03, 2002: Message edited by: Jessica Sant ]
 
Brett Swift
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about the 's they're supposed to be just ;'s .... I dunno what happened there!
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d1=(DerivedOne)p;
This should be legal at compile time but at runtime it may throw ClassCastException.
Am I right?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swati gupta:
d1=(DerivedOne)p;
This should be legal at compile time but at runtime it may throw ClassCastException.
Am I right?

Yes, that is correct.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brett Swift:
sorry about the 's they're supposed to be just ;'s .... I dunno what happened there!

If you ever notice the smiley's showing up you can edit your post and select the checkbox that says "Disable Smiley's in Post"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic