This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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

Exam cram(Mock)

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,
Can anybody help me regarding this question!
Suppose we have two classes defined as follows:
class ApBase extends Object implements Runnable
class ApDrived extends ApBase implements Observer
Given two variable created as follows;
ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();
Which of these will compile and execute without errors
A) Object obj = aBase;
Runnable rn = 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 given is B, but I think none of them is correct. A class can be converted into an interface only if that class or any of its superclasses implement that interface. Then how can B be correct. Can anybody explain me.
Regards
Kiran.

[This message has been edited by KN (edited August 13, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ApBase extends Object implements Runnable
ApBase aBase = new ApBase();
B) Object obj = abase;
Runnable rn = (Runnable) obj ;
Dont you think abase implements Runnable?
(should "abase" be "aBase"? Was it a typo ?)

[This message has been edited by vasansrini (edited August 13, 2000).]
 
KN
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vasansrini,
You are right, it was a typo. I have corrected it.
class ApBase extends Object implements Runnable
ApBase aBase = new ApBase();
I think it means that the class ApBase extends class Object & class ApBase implements Runnable. It doesn't mean that Object class implements Runnable. Correct me if I am wrong.
If you think that it means that Object implements Runnable then option A should also compile fine.
Thanks,
Kiran.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has got to be one of the most frequently misunderstood topics in Java. All you have to do is write some test programs and see what happens when you try to compile:
A) Object obj = aBase;
Runnable rn = obj;
The compiler sees that you are trying to cast a reference to Object to a Runnable type. The compiler DOES NOT KNOW OR CARE that the preceeding line assigned a compatible reference to obj.
You MUST get the difference between what happens at compile time and what happens at run time straight.

B) Object obj = aBase;
Runnable rn = (Runnable) obj ;
The compiler sees that you are deliberately casting obj to type Runnable and assumes that you know what you are doing. At RUNTIME the cast will be checked and if obj is not Runnable, a ClassCastException will be thrown.
Bill
 
KN
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill. First of all, I would like to say that you have written an excellent book. It's really helpful while preparing for SCJP.
I agree with your statement. It will compile fine. But the Question asked if it will compile & execute without errors. A check will be made at runtime to see if Object implements Runnable.It should throw an Exception as Object doesn't implement Runnable(am I right here!). But to my surprise it works fine. Can you please explain me why?
Regards,
Kiran.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kiran in
lets take ans A)
in line Object obj = aBase
the class ApBase is Upcasted to Object and hence only methods general to Object and APbase can be refrenced.if any methods specific to apbase is called Runtime exception will be thrown.
| ||ly Runnable rn = obj; compiles fine but in runtime it fails Bcuz
Obj now has only the Generalized methods of Object and Apbase.the specific methods specific to ApBase can only be Obtained when we Downcast a Object to Runnable (in this case) .
since proper downcasting is done only in case of B ,B is the Right Answer.
all others compiles correctly but fails in Runtime.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> A check will be made at runtime to see if Object
implements Runnable.It should throw an Exception as Object doesn't implement
Runnable(am I right here!). But to my surprise it works fine. Can you please
explain me why?
<<
Very important concept: Casting a reference does NOT CHANGE the type of the object referred to. OBJECTS DON'T CHANGE THEIR TYPE! Once created (at runtime) from a class that implements Runnable, the object referred to will always be a Runnable.
Bill
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, so if a sub-class inherits a super class that implements an interface, say Runnable, the subclass itself does not implement Runnable?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>Hi, so if a sub-class inherits a super class that implements an interface, say Runnable, the subclass itself does not implement Runnable?
<<
Sure it does, interfaces are inherited just like classes because if class A has the methods to implement interface X, then class B that extends A will inherit them.
Again - you have to make the distinction between what the compiler tries to do versus what happens at runtime.
See my first messge in this thread.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic