aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help me 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 "help me" Watch "help me" New topic
Author

help me

shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
suppose we have two classes defined as follows
class ApBase extends Object implements Runnable
class ApDerived extends ApBase implements Observer

ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();

which of the folllowing java code fragments will compile and execute without error?

a Object obj = aBase;
Runnable rn = (Runnable)obj;

b Object obj = aBase;
Runnable rn = (Runnable)obj;

c Object obj = aBase;
Observer ob = (Observer)aBase;

d Object obj = aDer;
Observer ob2 = obj;

the answer for this given is b.


I don't understand how to chose the right answer.please explain
what are rules for object reference conversion.

sh
Michael Burke
Ranch Hand

Joined: Jul 29, 2000
Posts: 103
Your a and b answer are the same to start off with.Object conversion and casting is allowed only from sub class to super class or super interface. In the first line of b your assigning Base to Object always allowed because every object inherits from Object. You can then assign the reference Object obj to Runnable as long as you cast obj to Base since this is the object obj is referring to and the Base class implements Runnable. The rules for object conversion can be found in any of the cert books, the RHE book of the Mughal book. Mughal goes in to much greater detail.
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
hi!
Is there any good site for this topic.I am just referring RHE book.Is it enough for certification exam
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
Hi! Shabbir
My personal opinion is that Khalid has a great explanation on this subject. He has put the rules together in one place. If you read that portion it would help you answer the questions like the one you have posted above with great ease. I sincerely think you should look at that chapter.I can e-mail the rules to you, I do not want to put it in this forum as I am not sure if I will be violating copyright laws.
Latha
Originally posted by shabbir zakir:
hi!
Is there any good site for this topic.I am just referring RHE book.Is it enough for certification exam

shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
hi!
My email address is shabbirzakir@yahoo.com.Please mail me the rules.I would be very thankful to you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help me