wood burning stoves*
The moose likes Java in General and the fly likes Cloning in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cloning in Java " Watch "Cloning in Java " New topic
Author

Cloning in Java

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
I have understood the concept of cloning but i have doubt in implementing it practically. The following code will show a compile time error :

class A {
private int x;
public A(int i) {
x = i;
}
}

public class CloneDemo1 {
public static void main(String args[])
throws CloneNotSupportedException {
A obj1 = new A(37);
A obj2 = (A)obj1.clone();
}
}

It says that this code triggers a compile error, because Object.clone is a protected method.

1. So my question is why should it trigger an error even if it's a protected method, because protected methods are avaialable to subclasses and Object class is the superclassof every class, so every class has access to clone method.

2. question : Why do we have add a clone method and in it write super.clone()???
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError(e.toString());
}



Thanks in advance .....
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Class A must implement the Cloneable interface


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

What Manuel is correct, and so is Siddharth Bhargava's statement about overriding clone(). You must do both for clone to be usable on instances of a class.

Why is it designed this way? Quite honestly, it's a horrible design, as has been discussed here many, many times. But remember that it's one of the original methods of the java.lang.Object class, the very, very oldest class in the Java API. The people who designed this mechanism had very little experience programming in Java!


[Jess in Action][AskingGoodQuestions]
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
And Cloneable is misspelled. It should be Clonable.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Manuel, It is very well Cloneable only.

Have a look at this url .


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by Raghavan Muthu:
Manuel, It is very well Cloneable only.

Have a look at this url .


Yes, I know. What I meant is that the Java creators misspelled the name when creating the concept and then it went to production misspelled (Cloneable doesn't exist in the English dictionary).
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


Originally quoted by Manuel Leiria :
Cloneable doesn't exist in the English dictionary


In that sense, it is right Manuel. As that context was absent in your previous reply, i thought you told it by mistake and just pointed out how it is defined and present in Java API.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cloning in Java
 
Similar Threads
Please help - cloaning members within a superclass and class.
Why PROTECTED method in class Object?
clone() mehtod
Understanding superclass and subclass objects
super.clone()