aspose file tools*
The moose likes Java in General and the fly likes Understanding on Clone object Creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Understanding on Clone object Creation" Watch "Understanding on Clone object Creation" New topic
Author

Understanding on Clone object Creation

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Hi Ranch,
I wrote the following code and my concern is that


1) when this line execute F1 f=new F1();
f reference variable object has Object o reference ,is I am correct ?
2) when this line execute f2=(F1)super.clone(); and clone method has Object of class Object as return type so is Object of class Object has Object o as reference variable ?
I am not getting this kindly help me on this.
Thanks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Hi Upanshu,

Why do you have static field F1 f2? Why do you have the Object o field? Why does your constructor (which should construct a new object) put a clone of that newly constructed object in f2? Why doesn't the toString method do anything with the current object? Why does your main class extend F1? It seems like you're having a hard time grasping the concept of object oriented programming.

Can you explain in your own words what an object is, and what you can do with it?
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Thanks Stephan for your inputs
Answers to your questions are below
1) Why do you have static field F1 f2?
Because I want the clone object to be referred by f2 reference variable of class F2 and also as I am using f2 in static method which is main if it is not static than error will be there " can't use non static from static context".
2) Why do you have the Object o field?
As the return type of clone method is Object so declared Object o field. Apology for that .
3)Why does your constructor (which should construct a new object) put a clone of that newly constructed object in f2?
Because I want a clone of the object that is referred by f. and created cloned object should be referred by f2.
4) Why doesn't the toString method do anything with the current object?
I have overridden the toString method because I want to see the effect of clone , to check whether the clone object has the same field as the original object .
5) Why does your main class extend F1?
Because as I am using f2 reference variable in System.out.println() method and if I don't extends class F1 then error will be there that can't find f2.

I know in my first post of this thread that code I mentioned has become little hotch potch ,kindly have a look on the answers for your question and is I am not clear on the object oriented programming concept .
Thanks

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

upanshu vaid wrote:I wrote the following code and my concern is that...

And my concern is that you don't understand how Cloneable works.

1. It is a horrible interface (and hopefully never to be repeated), because it does not define a clone() method; and, to this day, STILL doesn't properly explain how to implement a clone() method.

2. You implement Cloneable by adding a public clone() method, specifically:
public <something> clone() throws CloneNotSupportedException { ...
where <something> is the class you want to return (usually the one that defines the method); and, at the very least, calling:
super.clone();
inside it.
Anything else you want/need to do is up to you, and should be done after that initial call. In that way, it's rather like a constructor.

HIH

Winston

PS: And my apologies if the post sounds tetchy. Any "violence" is aimed at the designers of Cloneable; not you.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Winston, why public? Is there no merit in making a protected clone method?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I'm not Winston, but I would say that while there may be a legitimate use for a protected clone() method, the most likely case is that you want a public method. Unfortunately people often just make it protected, because that's what the Object implementation is. And then only later do they realize that they really wanted public. Note that the javadoc for Cloneable recommends overriding with a public method. The Object clone() method is protected only because they didn't want it to appear as public on all classes - only on those that chose to provide a working clone() method, by implementing Cloneable.

No, this doesn't really make sense, but as noted, that's because it's a stupid design. We're just dealing with the consequences of that.

I would also quibble with Winston's recommendation - there is no need to put "throws CloneNotSupportedException" in the overriding method, and I would strongly recommend not doing this. If you're implementing Cloneable, then you can guarantee it won't be thrown - so why not make it easier for people to use your method? Unless you want to throw the exception in certain circumstances, but that seems like an unlikely corner case. Anyway, if you don't put in the throws clause, you will have to catch in you overriding method. But it should never happen anyway. I would do something like this:
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

Mike Simmons wrote:I'm not Winston, but...

...you've pretty much corrected everything in my post that needed correcting.

The only thing I can add is that there are alternatives to Cloneable which are arguably even better (eg, a copy constructor or factory method).

But, as you probably gathered, I'm not a big fan of Cloneable.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Winston Gutkowski wrote: . . . I'm not a big fan of Cloneable.

Winston
Nor was Joshua Bloch; see Effective Java™ page 54
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

BTW...



There is no reason to use the super keyword here. Since you never overridden the clone() method, calling it via "clone()" and "super.clone()" goes to the same method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
jQuery in Action, 2nd edition
 
subject: Understanding on Clone object Creation