• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Declaration N access control

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi this is the Question from Hardtest
Question
Suppose we have two classes defined as Follows
class Apbase extends Object implements Runnable
class Apderived extends Apbase implements Observer
Given two variables created as Follows
Apbase aBase = new ApBase();
ApDerived aDer = new ApDerived();
which of the Followinf java statements will compile and execute without error?
a. Runnable rn = aDer;
b. Runnable rn2 = (Runnable) aBase;
c. Observer ob = aBase;
d Observer ob2 = (Observer) aBase;
answers given are a and b
But i guess only the correct answer is b,
Becuase my question is when a ADer is upcasted to Runnable rn all the implemented methods Observer cannot be accessible from Runnable rn.hence i assume the answer 'a' will be Wrong??
Please correct me if i am wrong??

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't ApDerived also implement (implicitly) Runnable because
its superclass does?
Chengx
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A class can be converted into an interface if that class or any of its superclasses implements that interface. In this case, superclass of ApDerived(i.e, ApBase) implements Runnable. So ApDerived can be converted to Runnable. So, Both A & B are correct.
Hope this helps.
Kiran.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A non-final class can be casted to an interface always according to RHC book. Then why not d is correct???pls help me, its sooooo confused.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
latha ,I think that is true for compilation only not for execution. FYI there are different rules for compilation and execution in casting.
 
reply
    Bookmark Topic Watch Topic
  • New Topic