Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help me

 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
My email address is shabbirzakir@yahoo.com.Please mail me the rules.I would be very thankful to you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic