Forums Register Login

casting interface to array

+Pie Number of slices to send: Send
Hello everyone,
I am new to Javaranch and this is my first posting. Actually, I wanted to report a possible error in RHE.
On page 117, Fig:4.10 (Table) Row 4, Column 3, it is stated that casting from an interface to an array is compilation error. In the table of Fig4.9 (about convertion rules), it is stated that convertion from an array to an interface is legal provided that the interface should be Serialzable or Cloneable. I thought if "converting" from an array to an interface is legal then logically "casting" in the opposite direction should also be legal. I tested as follows and the code compiled.

Sorry, if this is already reported. But I could not find it in the erreta page of RHE.
Ali
+Pie Number of slices to send: Send
Hi Jauhar,
Welcome to JavaRanch ; it's a great place!
I think your code works because <code>a</code> is really an <code>int[]</code>.
If you try the following you get a compile error:

The <code>interface Cloneable</code> is not compatible; can't be cast even though an array implicitly implements <code>Cloneable</code>
Jane
+Pie Number of slices to send: Send
It is worth mentioning here that <code>Cloneable</code> has no methods in it. It just serves as a tag to indicate the particular object can be cloned. A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.
Another such "stub-interface" is <code>java.io.Serializable</code>
Just my two cents worth...
Ajith


[This message has been edited by Ajith Kallambella (edited October 03, 2000).]
+Pie Number of slices to send: Send
 

Originally posted by Jane Griscti:
[B]Hi Jauhar,
Welcome to JavaRanch ; it's a great place!
I think your code works because <code>a</code> is really an <code>int[]</code>.
If you try the following you get a compile error:

The <code>interface Cloneable</code> is not compatible; can't be cast even though an array implicitly implements <code>Cloneable</code>
Jane[/B]


Hi Jane,
In your code, you tried to cast an "array of Cloneable" to an "array of int" which surely gives compile error. But I meant that casting a "Cloneable" or "Serializable" to any array type is NOT a compile error. e.g, the following code compiles successfully.

Of course, there is a runtime error (ClassCastException), but compilatoin is OK.
Jauhar
+Pie Number of slices to send: Send
Ooops ... sorry Jauhar. Based on your example, I thought you were trying to make the point that a Cloneable array could be cast to any array as the array type implicitly implements Cloneable. Sorry for the mixup.
You are right, you can cast an Interface to an array; at least, at compile time

------------------
Jane
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 592 times.
Similar Threads
narrowing convertion in case of final
casting
Array Conversions on Dan's Topic Exam
File Convertion
Confusion re casting
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:28:39.