Author Topic:   urgent reply..on downcast [test on monday]
psethura
ranch hand
posted May 06, 2000 03:49 PM         
class P
{
}
class C {}


P p = new P();
C c = (C) p /// Gives ClassCastExeption.

Under what circumstances does downcasting does not result in classcastexception

Herbert
unregistered
posted May 06, 2000 04:12 PM           
The following general rules applies to casting:

1. A reference to any object can be cast into a reference to an object of class Object.

2. A reference to to an object can be cast into a reference to an object of class C', if the actual class of the object is a subclass of C'
3. A reference to an object can be cast into a reference to an object of interface I, if the actual class of the object implements I, or is of interface type I' and I' is a subinterface of I, or if the object is an array type and I is the Cloneable interface.
4.A reference to an object can be cast into a reference to an object an array type(with element reference type T), if the object is of an array type(with element reference type T') such that T' can be cast into T.


You need to go through these rules carefully and understand them well. I might also hint to you that downcast does not result into a compile error, for the compiler assumes that you really mean to do it that way, but it may result into a runtime error if your the rules above have not been observed. I have seen a few mock question where you need to chose between runtime Exception and compile time.

Best wishes for your exam

regards,

Herbert

maha anna
bartender
posted May 06, 2000 04:14 PM             
Here in your qstn the 2 classes are UNRELATED. The 2 objects class types have to be related from the SAME BRANCH of the class hierarchy. Since thease 2 are classe references, one MUST BE SUBCLASS/SUPER OF THE OTHER in order to cast one to the other. If the 2 object's classes are TOTALLY UNRELATED then, the compiler WILL NOT allow.

regds
maha anna

Sunny
unregistered
posted May 09, 2000 10:58 AM           
I am also getting confused with downcasting problems. Especially when interfaces come in the picture. Can anybody explain me in detail (if possible, with examples) about legal downcasting and compile and run time error for these castings?

Thanks a lot in advance -

Ajith Kallambella
ranch hand
posted May 09, 2000 11:11 AM             
Here is one rule of thumb which I always follow-

Whenever you see a cast with the explicit casting
ie., of type obj1 = (obj1)obj2

think about how it behaves at runtime. Usually casts like this cause ClassCastException at runtime rather than raise compile time errors.

On the other hand, if it is an implicit cast ie., of type
obj1 = obj2
using assignment or expressions involving object references, think like a Java compiler to see if it compiles fine.

This has worked for me fine in all of my mock tests. Let me know if you come across a problem where you cannot apply this rule. I will then try to refine it!!.



Ajith

Sunny
unregistered
posted May 09, 2000 12:03 PM           
Ajith,

Thanks for the info - I understand this part. But some questions from Bill Brogden book bother me. I guess, I should ask those specifically. I do not have that book with me today. May be tomorrow, I will post those questions.

|