Questions tagged [cloneable]

163 questions
2
votes
1 answer

Scala collection.mutable.Cloneable purpose

While trying to clone a mutable collection, my initial approach was to use the clone() method on the mutable.Cloneable trait. However, this defers to the java.Object.clone implementation that creates a copy of the reference, not a deep copy. From…
Woodz
  • 1,029
  • 10
  • 24
2
votes
2 answers

Need some assistance with my logic in cloning an object

Before I posted this, I read some of the previous posts and I really don't see anything wrong with my logic. (I'm 3 hours on this already and it's probably gonna kill my happy hour) *I never want to know the answers, I enjoy working hard so maybe if…
Addy75
  • 167
  • 2
  • 10
1
vote
1 answer

Delegation of clone() method call in inheritance in java

1.Child class extends Parent class. 2.Child class implements Cloneable and overrides clone() method calls super.clone() 3.Parent class doesn't implement Cloneable interface neither it overrides clone() method. Output: Both parent and child class…
user4811324
1
vote
1 answer

Java implementing interface Cloneable on a inheritance structure

I got the following Questions on my code: public class Parent { ... } public class Child extends Parent implements Cloneable { ... @Override public Child clone() { return new Child() //deep copy } } Here are my…
1
vote
0 answers

How is Objcet.clone() knows which class must be cloned?

How is clone() method in Object class knows which class/fields must be cloned? For example how Objcet.super() can know that I need to serialize exactly A class or B class if I don't put any arguments in it? clone() defined as nativ son I can not see…
Denys_newbie
  • 1,140
  • 5
  • 15
1
vote
2 answers

Unable to understand clone

I have a simple program to clone a object , I googled the error "Exception in thread "main" java.lang.CloneNotSupportedException:" but need your help to understand the error, why am I not able to get clone of obj1? public class Test{ int a; int…
David Prun
  • 8,203
  • 16
  • 60
  • 86
1
vote
0 answers

Why I can not use protected method from a sub class?

clone() method in the Object class is protected, so why I have an error? public class Test { public static void main(String[] args) { Test2 c1 = new Test2(); Test2 c2 = (Test2) c1.clone(); // error: clone() has protected…
Denys_newbie
  • 1,140
  • 5
  • 15
1
vote
1 answer

Why I have an error, but not an exception?

documentation Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown. Why I have an error clone() has protected access in…
Denys_newbie
  • 1,140
  • 5
  • 15
1
vote
0 answers

Why can't we achieve deep cloning through getters in java why we need prototype design pattern with Cloneable interface?

I need to know in a case of we need to clone an instance deeply why can't we achieve that using the getter method of our original object why we need any specific design pattern or interface like Cloneable ?
Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37
1
vote
1 answer

Is there a best way to clone a model for changing just one entry?

I have a model with some fields, and I'd like to add a new entry in the database of this model, but with changing only one field. Is there a best way to do so, without having to create a new instance and setting one by one each field ? Case : public…
Cyril N.
  • 38,875
  • 36
  • 142
  • 243
1
vote
0 answers

How to create Immutable object containing list of another mutable objects in java?

Say I have Employee class and within that I have a member like List of Address. One way is to create like list = Collections.unmodifiableList(list) and another way is doing defensive copying or deep cloning. But first approach is just wrapper around…
implosivesilence
  • 536
  • 10
  • 24
1
vote
1 answer

Do subclasses need to implement Cloneable?

Do subclasses of a class which implements Cloneable need also implement it explicitly? I read some discussions about this (Do subclasses inherit interfaces?) but the answers are not very clear.
webpersistence
  • 894
  • 3
  • 12
  • 29
1
vote
1 answer

Making LinkedBinaryTree Cloneable

I have implemented the LinkedBinaryTree structure and I want to make that tree cloneable, yet I couldn't figure out how to insert the Positions into the new tree properly. Here is my Position interface: public interface Position extends…
M.Soyturk
  • 340
  • 3
  • 14
1
vote
1 answer

Overriding Clone() method in Java

I know I should implement the Cloneable interface and then override the clone() method of the Object class in Test, and this is not my problem . I just do not understand why compiler gives "clone() has protected access in object" error while the…
John
  • 201
  • 1
  • 7
1
vote
1 answer

Why doesn't CopyOnWriteArraySet implement the Cloneable interface, while CopyOnWriteArrayList does?

In this bug report, Doug Lea writes (referring to a pre-release version of JDK 5.0): While CopyOnWriteArraySet is declared Cloneable, it fails to define public clone method. But it eventually ends up that CopyOnWriteArraySet doesn't implement the…
LrnBoy
  • 373
  • 1
  • 2
  • 9