• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

super.clone()

 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {

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

}

why this downcast works?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, there's no return statement and no exception handling, but that's not the problem.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic