• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding on Clone object Creation

 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston, why public? Is there no merit in making a protected clone method?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . I'm not a big fan of Cloneable.

Winston
Nor was Joshua Bloch; see Effective Java™ page 54
 
Henry Wong
author
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic