This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super.clone()

 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {

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

}

why this downcast works?
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, there's no return statement and no exception handling, but that's not the problem.
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is?
Is this a game of "I have problem, but I'm not going to tell you"?
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* 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!
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic