The clone method and the Cloneable interface - 1

"Hi, Amigo!"

"Hi, Kim."

"I'm going to tell you about the clone() method."

"The point of this method is to clone an object, or in other words, to create a clone/copy/duplicate of the object."

"When this method is called, the Java virtual machine creates and returns a duplicate of the object it is called on.

The Object class's implementation of the clone method is very primitive: only one new object is created, and the values of the original object's fields are assigned to its fields.

If the copied object contains references to other objects, then those references will be copied. Duplicates of those objects will not be created."

"Hmm. That's not much to go on."

"The thing is, the Java virtual machine doesn't know which objects can or can't be cloned. For example, files can't be cloned. The same is true for a System.in stream."

"So, the question of full-fledged cloning was punted to a class's developers. "This is all similar to how the equals method is handled. There's even something comparable to hashCode: the Cloneable interface."

"The Cloneable interface is what's called a 'marker interface': it doesn't have any methods and is used to mark certain classes.

"If a class's developer believes objects of the class can be cloned, he marks it with this interface (i.e. makes the class implement Cloneable)."

"If the developer doesn't like the clone method's standard implementation, he must write his own that will create a duplicate object in the right way."

"When you call the clone() method, Java checks whether the object supports the Cloneable interface. If it does, then it clones the object using the clone() method; if not, it throws a CloneNotSupportedException."

"In other words, we must either override the clone method or make the class implement Cloneable?"

"Yes, but you still have to override the method. The clone() method is declared as protected, so it can only be called by classes in its package (java.lang.*) or their subclasses."

"I'm a little confused—so what do I need to do to clone an object?"

"If you want to use the Object class's «default» cloning method, you need to:

"a) Add the Cloneable interface to your class"

"b) Override the clone method and call the superclass's implementation within your implementation:"

class Point implements Cloneable
{
 int x;
 int y;

 public Object clone()
 {
  return super.clone();
 }
    }

"Or you can write a implementation of the clone method entirely on your own:"

class Point
{
 int x;
 int y;

 public Object clone()
 {
  Point point = new Point();
  point.x = this.x;
  point.y = this.y;
  return point;
 }
}

"That's an interesting method. I'm sure I'll use it. Occasionally…"

undefined
12
Task
Java Core, level 9, lesson 3
Locked
TableAdapter
Edit the TableAdapter class so that it adapts the ATable interface to the BTable interface. The getHeaderText method should return "[] : ". For example, "[Amigo] : DashboardTable".
undefined
12
Task
Java Core, level 9, lesson 3
Locked
Adapter
Use the AdapterFileOutputStream class to adapt FileOutputStream to the new AmigoStringWriter interface.
undefined
20
Task
Java Core, level 9, lesson 3
Locked
Adapting multiple interfaces
Adapt IncomeData to the Customer and Contact interfaces. The adapter class is IncomeDataAdapter. Initialize countries before running the program. Mapping between country codes and country names: UA Ukraine US United States FR France If necessary, pad phone numbers with zeros to get 10 digits long (s
undefined
20
Task
Java Core, level 9, lesson 3
Locked
Yet another adapter
Adapt Scanner to the PersonScanner interface. The adapter class is PersonScannerAdapter. Create a private final Scanner field called fileScanner in the adapter class. Initialize the field in a constructor with one Scanner parameter. The file stores data in the following format: John Michael Peterson
undefined
20
Task
Java Core, level 9, lesson 3
Locked
Reinforce the adapter
Adapt Customer and Contact to RowItem. The adapter class is DataAdapter. Initialize countries before running the program. Mapping between country codes and country names: UA Ukraine US United States FR France
Comments (4)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Niko Level 41, Wuhan, China
28 March 2022
java对象如果想被克隆,它对应的类需要implements标志接口Cloneable。如果不重写clone()方法,则在调用clone()方法实现的是浅复制(所有的引用对象保持不变,意思是如果原型里这些对象发生改变会直接影响到复制对象)。重写clone()方法,一般会先调用super.clone()进行浅复制,然后再复制那些易变对象,从而达到深复制的效果。
Ibrahim Level 41, Sheffield, United Kingdom
17 January 2022
Check these videos out for further info on cloning: https://www.youtube.com/watch?v=b2uFL4BFDYg https://www.youtube.com/watch?v=WIh-TVq4ifI https://www.youtube.com/watch?v=KWbr7B5LDzs and this webpage: https://www.geeksforgeeks.org/clone-method-in-java-2/
Tian Pro Level 23, Cape Town, South Africa
5 March 2020
Occasionally...