Originally posted by Waez Ali:
I am really sorry for that,I just misunderstood
No worries; there's never any need to apologize for asking questions or making mistakes. If my post seemed to be chiding you, please don't take it as such. I enjoy helping people learn; that's why I hang out here.
Is there any other way to create clone object without overriding clone() method(i mean just using it, as it is available to this class)?
Technically no. If you want to be able to have
other methods call clone() on an object, you must override Object.clone()
and give it public access.
As you can see from your second example (TestCloneable), you can declare clone() to be protected, but then only TestClonable or one of its subclasses can call clone(). I don't think this is what you want in general. If you do it this way, you have to provide some other public method in the class (probably a static helper method) that calls clone() on the actual object.
That's just busy work that is best avoided by declaring clone() public in the first place.
Here are a couple pointers about your code that you don't need to understand just yet, but if you do all the better. If these are cnofusing, ignore them for now.
Your clone() methods don't need to declare "throws CloneNotSupportedException" since, clearly, you support cloning. Instead, have your clone() method catch CloneNotSupportedException so callers won't have to. This isn't a big deal, however. When overriding a method, you can remove exceptions from the parent's throws list but not add any. This is called "narrowing" since it shrinks (or narrows) the list of possible error conditions the caller must expect.
You should generally declare clone() as public so it can be called by other classes. When overriding a method you may declare the access level as more liberal than the parent's method, but not more strict (protected -> coderanch, private -> protected, but not protected -> private). This is called "widening" the access level, though I believe there's a better term (relaxing?) as you're allowing the method to be called by a larger set of classes (widening).
How "a"[][](2Dim) can be assigned to "obj"[](OneDim)array at line 2 ?
This is going to get complcated, so hang on!
a is an int[][], an array of integer arrays -- each element of the array a (a[0], a[1] and a[2]) is an int[]int[], an array of integers, is a subclass of Object (though not and Object[])obj is an array of Objects, so each element will accept any subclass of Object The above mean "Object[] obj = (Object[])a.clone()" is legal. In the same sense, you cuold also write
The output should be "a[0][1] = 2".
Note that the code in the for-loop has the possibility to throw ArrayIndexOutOfBoundsException, but it doesn't in this case. To see this, change the original array declaration by removing one of the elements from the third array {-1,0,2}. It might help to change line 5 to
This will sound circular, but the only reason you'd need to make clones is if you need to make clones.

All of the Collection classes (List, Map, and Set implementations) support cloning since that's a very common
pattern.
For example, you have a List of Sales objects (String item, double price, int totalSales) that is kept up-to-date while the application runs. Every so often you want to send the Sales List to another program/file containing only the items that have sold more than 20 items.
You clone the original List since you don't want to modify it. Instead, you work on a copy (clone) of the original. After you send it/save it to disk, you throw it away. The original List keeps tracking all sales figures.
[ January 29, 2005: Message edited by: David Harkness ]