Can you actually compile that code? There are at least two errors I can see that should be reported at compile time: 1. CloneNotSupportedException is not handled or thrown and 2. the method does not have a return statement.
Originally posted by Pavel Kubal: the question is "Why does downcast work in this case?".
Because I thought, that super.clone() returns parent object.
Because the clone returns an object with the same type of the cloned object. The method performs the "Shallow
Copy" on the current object which type is HelloWorld not Object.
This is what the documentation of the clone method says:
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the
object. The general intent is that, for any object x, the expression: x.clone() != x will be true, and that the expression: x.clone().getClass() == x.getClass() will be true, ...
Best regards, [ December 11, 2005: Message edited by: Nadeem Awad ]
Share Knowledge to gain it.<br /> <br />SCJP 1.2, SCDJWS 1.4, SCWCD 1.4, SCBCD 1.3, ICAD
Pavel Kubal
Ranch Hand
Joined: Mar 13, 2004
Posts: 356
posted
0
Well, many thanks for your answer. But in documentation is clone on some Object subclass. But I wrote super.clone()...it would imho be logical, that it clones parent object. Where am I wrong?
But it returns copy of current object, I know that, but I don't understand why there is used super.clone() instead of logical this.clone().
Do I make myself clear?
I know, this is elemental knowledge, but I would like to know how exactly it works
I'm not sure how cloning is actually implemented because it is implemented as a native method.
But it returns copy of current object, I know that, but I don't understand why there is used super.clone() instead of logical this.clone().
this.clone() would be recursive call and would be of no real use. The shallow copying is somehow implemented as a native method. It is defined in the Object class. You extended it (implicity tho'). super.clone() is going to invoke it.
* There is no such thing as the "parent" object. Perhaps this is the root of your confusion.
* Nadeem was quoting the relevent part of the documentation when he wrote that "x.clone().getClass() == x.getClass()", and later the documentation repeats, "this method creates a new instance of the class of this object".
This is the documentation in class Object, so you should realize the implication of that: any subclass (direct or indirect) should obey this as well!
Pavel, by "works" I take it that you mean it compiles successfully.
True, the code you gave will compile successfully because compile-time checking of casts is limited--the most the compiler can do is to make sure that the reference being cast is at least a supertype. Since Object is a supertype of the Test class, the compiler will allow the statement. Another check will be made at runtime to ensure that the cast is actually valid. If the runtime check fails, a ClassCastException will be thrown. For the code you gave, however, the runtime check will not even be made because a checked exception (CloneNotSupportException) will be thrown by Object.clone() first.