Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

help me

 
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
 
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
 
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.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic