This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes super.clone() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super.clone()" Watch "super.clone()" New topic
Author

super.clone()

Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
public class Test {

protected Object clone() {
Test test = (Test)super.clone();
}

}

why this downcast works?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4792
    
    8

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Ok, there's no return statement and no exception handling, but that's not the problem.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
So what is?
Is this a game of "I have problem, but I'm not going to tell you"?


Tony Morris
Java Q&A (FAQ, Trivia)
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Did you read the question?

Here's proper code...almost the same as previous.



And the question is "Why does downcast work in this case?".

Because I thought, that super.clone() returns parent object.
Nadeem Awad
Ranch Hand

Joined: Jan 09, 2005
Posts: 74
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
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
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
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.


Arvind
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
* 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!


There is no emoticon for what I am feeling!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4792
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super.clone()