File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cloning in Java

 
Siddharth Bhargava
Ranch Hand
Posts: 274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class A must implement the Cloneable interface
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Manuel Leiria
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Cloneable is misspelled. It should be Clonable.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel, It is very well Cloneable only.

Have a look at this url .
 
Manuel Leiria
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic