This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepa,
I am in a middle of a personal work. I come back soon.
regds
maha anna
 
gunjan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gunjan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gunjan & kuei for the detailed reply.
I appreciate that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic