Questions tagged [cloneable]

163 questions
0
votes
1 answer

When overriding the clone() method, why do you need to declare it public?

In a java book I'm reading, a practice program takes the protected clone() method in the Object class and overrides it. When they do this tho, they expand the visibility modifier to public so it can be used in any package. I'm confused why this…
Ryan Foster
  • 103
  • 9
0
votes
1 answer

What is the use of Clonaeable interface in java?

Please don't close as duplicate. I know there are multiple threads on this topic but none of them answers my question. I am still struggling to understand why do we need Cloneable interface in java. If we want to create copy of an object, we can…
0
votes
2 answers

How does implementing Cloneable interface allows cloning of objects, as it is a Marker Interface and doesn't have any methods?

I am having a class whose object I want to clone. I did that by implementing the Cloneable interface and overriding the clone method. But if I am creating a clone method, without implementing the Cloneable interface, it is throwing an…
Nayan Arora
  • 47
  • 1
  • 7
0
votes
0 answers

How do I transfer an array from one class to another, specifically the int field in the source class array

I implemented compareTo in my Student class which implements Comparable which has a private int age field. I also have a Classroom class which has a classroom array that takes in Student objects. I have my classroom array and I want to send it's…
mobcity zkore
  • 263
  • 1
  • 8
0
votes
1 answer

require Cloneability of template parameters in Java

I've this Java generic class, and I'd like to clone it, in order to perform a deep copy of it. Now, I thought this following code would work, but in the clone() method I cannot call clone() for every member which is not a primitive type. I tried to…
eToTheEcs
  • 1
  • 1
0
votes
2 answers

Java deep copy difference between a String and Date object

How to create a deep copy for a date object, for example birthDate of a student? how will copying a date object different from Name or age? Here is an example for cloning I got from net. import java.util.Date; public class Clone { public static…
Vinod
  • 675
  • 3
  • 7
  • 25
0
votes
0 answers

How to clone an object from an array of objects with different types

I've got this code: int width = 1280; int height = 720; List devices = new ArrayList(); class ElectronicDevice { int x; int y; // ... } class EClock extends ElectronicDevice { int hz; EClock(int x,…
lqdev
  • 403
  • 3
  • 12
0
votes
1 answer

how to make the generice data type accessable to clone() method

enter image description here as shown in the figure there is an error at 32 line that says Node(Object) is not defined because clone() returns an Object data Type I tried to make another constructor that takes an Object data type then cast…
user7227263
0
votes
2 answers

Is clone() method is equivalent to pass-by-value concept in Java

Clone() method in Java provide the way to clone one object into another object, so that changes made by second object won't reflect in first object. My question is that is using clone() and using pass-by-value are equivalent or there are differences…
codex
  • 1
  • 1
0
votes
3 answers

Instance var referring to an object

Instance variables go on stack and objects go on heap and object references go on stack. Right? But what if an instance variable was a reference to an object? Like var c: class clony implements Cloneable { clony c = new clony(); @Override …
mike
  • 45
  • 5
0
votes
1 answer

Duplicating button bar without cloning

I am having an issue duplicating a button bar without making and exact clone of the first button bar. In my attempt, the second button bar that forms does not work properly. When the buttons are clicked, they do nothing. I want all duplicated button…
EBM84
  • 37
  • 4
0
votes
2 answers

java Cloneable: Base class implements it incorrectly; what to do?

I'm using a class (let's call it the BaseClass) from a package which implements the Cloneable interface, but it appears to do so by creating a new object and not by calling super.clone(). I have made a SubClass of this BaseClass, which then crashes…
0
votes
2 answers

Prototype Creation Pattern

In java we have and interface cloneable What I want to understand is why abstract class implements that interface there is still no implementation of clone() method of interface in abstract class ?
Waqar Ul Khaf
  • 569
  • 1
  • 3
  • 15
0
votes
3 answers

Downcasting while calling super.clone() method

Consider the following program class A implements Cloneable { String str = null; public void set(String str) { this.str = str; } @Override public A clone() { A a = null; try { …
victini
  • 193
  • 1
  • 6
0
votes
2 answers

Why modification in Arraylist copy, modifies the original?

I am trying to copy the contents of an ArrayList to another and change the contents of copy. I don't want this to be reflected in the original. I checked on SO and made changes accordingly, still the same issue. Can someone help? I am sharing the…
Kṛṣṇa
  • 91
  • 1
  • 8