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
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).]
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Joined: Oct 03, 2000
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.
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