Author | Topic: urgent reply..on downcast [test on monday] |
psethura ranch hand |
posted May 06, 2000 03:49 PM
class P { } class C {}
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'
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
|
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 On the other hand, if it is an implicit cast ie., of type 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!!.
|
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.
|
| | |