aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Parent and child class relationship. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Parent and child class relationship." Watch "Parent and child class relationship." New topic
Author

Parent and child class relationship.

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
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

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
John Loney
Greenhorn

Joined: Jun 03, 2002
Posts: 4
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

Joined: Jul 13, 2001
Posts: 94
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

Joined: May 22, 2002
Posts: 61
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

Joined: May 22, 2002
Posts: 61
sorry about the 's they're supposed to be just ;'s .... I dunno what happened there!
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
d1=(DerivedOne)p;
This should be legal at compile time but at runtime it may throw ClassCastException.
Am I right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parent and child class relationship.