What is the use of the cloned interface in java?

What is the use of implementing a cloned interface since it is a token interface?

I can always make a public Object clone () method in my class. What is the actual purpose of the cloned interface?

+6
source share
5 answers

This is because the clone() method throws a CloneNotSupportedException if your object is not Cloneable .

You can see the documentation for the clone() method .

The following describes the clone() method in the Object class:

 protected Object clone() throws CloneNotSupportedException 

Note:

It was also recognized that Clone violated. This answer here at SO explains why and how you can avoid using it.

+4
source

Creating a Cloneable Token Interface was a mistake.

However, the only thing it does is to β€œenable” the default implementation of clone() in Object . If you do not implement Cloneable , then calling super.clone() will raise a CloneNotSupportedException .

+1
source

Some people say this is an attempt to mimic a copy constructor from C ++, but here is a previous similar question about StackOverflow about this: About Java cloneable

+1
source

The target is listed in javadoc . This means that cloning an object of this type is allowed.

If your class uses the built-in implementation of clone() (provided by the Object.clone() method), then this marker interface allows phased cloning. (If you call the clone built-in method on an object that does not implement Cloneable , you get a CloneNotSupportedException .)

+1
source

The purpose of the clone () method is to create a new instance (copy) of the object on which it is called. As you can see in the answers to using the clone method, your class should implement the Cloneable interface. You can choose how to implement the clone, you can make a shallow or deep copy for your class. You can see examples at http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/ .

+1
source

Source: https://habr.com/ru/post/920475/


All Articles