cloneable

super.clone() operation not works in Derived Class

谁说胖子不能爱 提交于 2019-12-14 03:56:40
问题 This is raised because of the technical difficulties faced in my Project. Problem: I need to clone a Object of a Class where it extended the properties(Inheritance) from a third party library class(where we don't have access to modify its contents) Let me explain with example below: Parent Class: public class UnChangeableBaseClass { //fields and Methods } Child Class: class DerivedLocalClass extends UnChangeableBaseClass implements Cloneable { // local fields and methods public Object clone()

Does cloning provide a performance improvement over constructors/factory methods?

人走茶凉 提交于 2019-12-05 05:38:29
I'm maintaing an older Java code base (jvm 1.4) that seems to use cloning as an alternative to object instantiation, I'm guessing as a performance optimization. Here's a contrived example: public class Foo { private SomeObject obj; // SomeObject implements Cloneable public Foo() { obj = new SomeObject(); obj.setField1("abc"); // these fields will have the same value every time obj.setField2("def"); } public void doStuff() { SomeObject newObj = obj.clone(); // clone it instead of using a factory method // do stuff with newObj } } The usual caveats about premature optimization notwithstanding,

Why Object clone() method available only to classes that implement Cloneable interface? [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-02 22:16:39
This question already has an answer here: Why is the clone() method protected in java.lang.Object? 11 answers I know that clone() is a protected method, but "protected" means that it is accessible for all subclasses of particular class. Any Java class is a subclass of Object , so what is the reason for the protected method here? And why can we call clone() only on classes that implement the Cloneable interface? I can't understand how it connects to the fact that clone() in Object is declared as protected. Object's clone() method is quite special, as it always returns an instance of the current

Has the design of marker interfaces like Java's Serializable or Cloneable evolved in C#?

孤街醉人 提交于 2019-12-02 02:29:17
Java provides java.io.Serializable and java.lang.Cloneable in his standard library (and special support for it in the language and the JVM) for tasks around deserializing/serializing/cloning. Has C# chosen a different path to provide this functionality, how does the implementation and code using it differ from Java and why was it done this way? As an example, why does C# use both an attribute (annotation) and an interface for serialization? .NET doesn't use ISerializable as just a marker interface. It acts not only as a marker, but also allows you to control exactly how .NET will serialize the

Why no default clone() in Cloneable in Java 8

微笑、不失礼 提交于 2019-11-30 13:56:15
问题 Cloneable in Java is inherently broken. Specifically, my biggest problem with the interface is it expects a method behavior that doesn't define the method itself. So if traversing through a Cloneable list you must use reflection to access its defined behavior. However, in Java 8, we now have default methods and now I ask why there isn't a default clone() method in Cloneable . I understand why interfaces cannot default Object methods, however, this was an explicit design decision and so

Why no default clone() in Cloneable in Java 8

荒凉一梦 提交于 2019-11-30 08:55:59
Cloneable in Java is inherently broken. Specifically, my biggest problem with the interface is it expects a method behavior that doesn't define the method itself. So if traversing through a Cloneable list you must use reflection to access its defined behavior. However, in Java 8, we now have default methods and now I ask why there isn't a default clone() method in Cloneable . I understand why interfaces cannot default Object methods , however, this was an explicit design decision and so exceptions can be made. I sort of envision deprecating Object.clone() and changing its interior code to

Why is Cloneable not deprecated?

情到浓时终转凉″ 提交于 2019-11-28 02:59:58
It is commonly understood that Cloneable interface in Java is broken. There are many reasons for this, which I will not mention; others already did it. It is also the position of Java architects themselves. My question is therefore: why has is not been deprecated yet? If the core Java team have decided that it is broken, then they must also have considered deprecation. What are their reasons against doing so (in Java 8 it is still not deprecated )? There is a bug submitted in 1997 to Java Bug Database about adding clone() method to Cloneable , so it would no longer be useless. It was closed

Proper way to deep copy with copy constructor instead of Object.clone

两盒软妹~` 提交于 2019-11-27 16:37:53
问题 I have some code that performs a deep copy using Object.clone, but I'm trying to rewrite it using the more "acceptable" copy constructor technique. Below are two simple examples of what I'm trying to do, the first using clone and the second using a copy constructor. Deep copy using clone import java.util.*; abstract class Person implements Cloneable { String name; public Object clone() throws CloneNotSupportedException { return super.clone(); } } class Teacher extends Person implements

The method clone() from object is not visible?

感情迁移 提交于 2019-11-27 05:36:19
问题 Question: package GoodQuestions; public class MyClass { MyClass() throws CloneNotSupportedException { try { throw new CloneNotSupportedException(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { MyClass obj = new MyClass(); MyClass obj3 = (MyClass)obj.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } } Here class 'MyClass' can able to clone its own object by calling the clone method in 'Object' class. When I try to clone

Confusion about cloneable interface and object.clone() in java

前提是你 提交于 2019-11-27 02:39:52
问题 If I have: class foo implements Cloneable and then do: bar = new foo(); bar.clone(); I get a shallow copy without needing to write any bar.clone() code like I normally would need to do when I implement an interface. My understanding is that an interface's functions must be filled in by the class implementing it, and Object.clone() has no implementation (as per the docs, "The class Object does not itself implement the interface Cloneable") So where does my shallow clone come from? Where is the