wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting" Watch "Casting" New topic
Author

Casting

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello Maha/tony,
Can you please explain this by step by step about the results. I would really appreciate it.
Thanks
deepa
class ApBase extends Objects implementsRunnable
class ApDerived extends ApBase implements Observer
//Following variables created from above classes
ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();
which of the following compile and run w/o error ?
A) Object obj = aBase;
Runnable rn obj;
B) Object obj = aBase;
Runnable rn = (Runnable)obj; ..........Answer
C) Object obj = aBase;
Observer ob = (Observer)aBase;
D) Object obj = aDer;
Observer ob2 = obj;
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Deepa,
Can you tell us which part you don't understand?. I think that way we know what is expected from you. What do you think?. Feel free to explain.
regds
maha anna
Deepa sivasankar
Ranch Hand

Joined: Jul 22, 2003
Posts: 35
class ApBase extends Objects implementsRunnable
class ApDerived extends ApBase implements Observer
//Following variables created from above classes
ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();
which of the following compile and run w/o error ?
A) Object obj = aBase;
Runnable rn obj;
B) Object obj = aBase;
Runnable rn = (Runnable)obj; ..........Answer
C) Object obj = aBase;
Observer ob = (Observer)aBase;
D) Object obj = aDer;
Observer ob2 = obj;

My understanding is this:
in A, Object obj = aBase is OK, because aBase extends from Object. All objects are extended from Object.
Runnable rn = obj;
Rule for compile time says, if new type is an interface, old type is a class then old type must implement that interface. But here obj is of type aBase right. aBase implements runnable. Then why is this wrong?

in B, Runnable rn = (Runnable) obj;
At compile time, obj is casted to Runnable so it is OK at compile time. At runtime, it is always OK from non-final class to interface. Here is obj is a non-final class. Is my explanation right?
in C, Observer ob = (Observer) aBase;
at compile time, it is not OK, because aBase doesn't implement Observer interface .IS this right?
in D, Observer ob2 = obj;
at compile time, old type must implement the Observer interface.obj is of type aDer, aDer implements Observer interface right. Then why is this wrong?
Thanks for your help, i appreciate.
deepa
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In choice A --
line 1. Object obj = aBase;
line 2. Runnable rn = obj
Line 1: converting ABase to Object is ok
Line 2: Since Obj is now an Object b/c of the conversion - it is 1 step higer in the "hierarchy" -- So, now, in order for the Object to compile properly into that interface, it needs a cast ( similiar to a "narrowing" conversion in primitives in that you need to use a cast to get from a long to a int)... which is provided by choice B. --- It looks like you're looking at the wrong rules. It should be casting rules, not conversion.
Hope this is helpful.

[This message has been edited by maha anna (edited April 20, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am confused. What is the difference between casting and converting.
int i = 10;
float j= 0.0f;

j = (float) i;
If you have two difft types, then you use cast to convert to
target type.
I don't know what is converting rules???
Can anyone explain in detail the first Question in the thread please.
THanks
Deepa

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Deepa,
I am in a middle of a personal work. I come back soon.
regds
maha anna
gunjan
Ranch Hand

Joined: Jan 28, 2000
Posts: 33
Deepa:
A]In the first case, the obj of type Object. The true nature of the class that the obj reference points to is ApBase . But at compile time the true nature of the class is not evaluated. Checks are made only between the reference type. Here you are trying to assign a Object type to Runnable.
Java is a strongly typed language. It does not allow assignments of subclasses or interfaces to superclasses. You have to use the casting operator
B] This is OK. At compile time you can always cast between any non final object and interface. But at run time if the new type is a interface, then the class must implement that interface. Both of which are happening here. So it is right.
C]At compile time you can always cast between any interface and any non final object. There is no problem. However the rule at runtime says that if the new type is a interface then the class must implement that interface. And hence is a error
D]Here you are not casting. It is a conversion. The rules for conversion say that if the new type is a interface, the class on the right hand side must implement the interface
Regards
Gunjan

Regards<BR>Gunjan
gunjan
Ranch Hand

Joined: Jan 28, 2000
Posts: 33
Deepa:
It really helps to think that there are two criteria that you have to check
1. Whether it is conversion. If it is, then there are only compile time rules
2. Whether it is casting. If yes then there are compile time and run time rules
Regards
Gunjan
Deepa sivasankar
Ranch Hand

Joined: Jul 22, 2003
Posts: 35
Gunjan,
I appreicate your help.
I still have the doubt in D.
in D, Observer ob2 = obj;
at compile time, old type must implement the Observer interface.obj is of type aDer, aDer implements Observer interface right. Then why is this wrong?
Thanks
deepa
gunjan
Ranch Hand

Joined: Jan 28, 2000
Posts: 33
Deepa:
This is the case of conversion. At compile time the true nature of the class is not evaluated. The check is made between reference types.
Therefore at compile time obj is of type Object and hence it is wrong to assign it to Observer type as Object does not implement Observer.
Regards
Gunjan
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Gunjan & kuei for the detailed reply.
I appreciate that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting
 
Similar Threads
casting
reference assignment and casting question
Reference conversions.... Please help
casting problem from bill brogden mock exam
help me