|
Re: Class Conversion problem
|
Posted: Nov 10, 2003 6:18 AM
|
|
Hello Mr. Kelvin You are changing your question in every post. Your first post was not clear with your first example and that is why I gave you a generic reply. In second post you changed your second example. Anyway, here is complete explanation of your question.
1. When you cast an object reference to a particular class then Java trusts you only 50%. This means Java lets you compile the statement, but it double checks the class casting at runtime. Here is an example,
X x = (Z)y;
Here, we are trying to cast a reference variable y to type Z type. At compile time Java checks if an object of Z type is asignment compatible to an object of type X. If answer is yes then it lets you compile the code. Note that compiler doesn't check what kind of object reference variable y is referring to. It just checks for assignment compatibility between Z and X. This is so because compiler has no knowledge about the real type of object reference variable y will refer at runtime. So, compiler trusted us 50%.
However, at runtime, Java knows the actual object type reference variable y is refers to. At runtime Java will check: "Can an object of class of y be assigned to an object of class Z?". If answer is yes then it will go ahead with cast. Otherwise, it will throw classcast exception.
2. Let us discuss your problem now. A object reference of subclass can always be assigned to a refercne variable of superclas type. For example, B b = new A();
This is fine because A is subclass. However, you cannot say, A a = new B(); because B is super class. In your post, you have
B b = new A();
A a = (A) b;
First statement is ok. For second statement, compiler checks if an object of type A can be assiged to an object of type A. At compile time it passes the check. At runtime, Java performs a check again. It checks the class of actual object referred to by reference variable b. Since you have assigned a reference of class A to b, runtime will find that b is referring an object of class A. Now, it will check if an object of type A ( it is b) can be assigned to an object of type A ( (A)b part of second statement). The answer is yes. So, your second statement is successful.
My reply was not the same in my first post because you din't include first statement of your first example in your post. That is why I chose to give you a generic answer.
Let us change your statement and see what happens.
B b = new B();
A a = (A) b;
compiler will be fine with second statement. At runtime, Java will find that b ( in A a = (A)b) is referring to an object of type B. However, an object of type B (superclass) is not assignment compatible to an object of type A (subclass). And, you know what happens afterwards. ClassCastException!!!
2. Let us discuss array assignment compatibility now. If you are trying to cast an array object to some other array object type then think for a moment - "Is casting ok if my objects are not array rather simple object?". If anwser comes yes then you are fine. Otherwise, Class cast execption again. So, here is an example. If you want to know if teh following statement will work
X[] x (Z[])y;
then just take out brackets and see if
X x = (Z) y;
works then array casting will also work. In case of array you are dealing with multiple element of the same kind. If something doesn't work with one element of a type then how can it work with multiple elements of the same type. In fact, for array casting the logic is same. If an object of type Y can be assigned to an object of type Z then (Z[]) y is fine when y is an array of type Y.
4. Now let us discuss your second example,
It is ok if you write
B[] b = new A[10];
A[] a = (A[]) b;
But, if you write
B[] b = new B[10];
A[] a = (A[]) b;
then it will compile, but at runtime Java will find that b is an array of type B and an object of type B cannot be assigned to an object of type A.
I hope this clears your doubt about class casting. Thanks Kishori
|
|