example of a class that implements Cloneable Java [duplicate] - java

This question already has answers here:
How does Cloneable work in Java and how do I use it?
(6 answers)
Closed 6 years ago.
From what I have read about the Cloneable interface its a waste of time but we still have to study it for some reason. I have been going through sample questions but can not find an answer to the one below.
Example of a class that implements clonable?
Anyone have an example of a class that implements cloneable?
---edit---
This question is not a duplicate as this question clearly states that I'm looking for an "Example of a class that implements cloneable?"
The question that ye state it is a duplicate of(stackoverflow.com/questions/4081858/about-java-clone‌​able) does not ask for an example and I had read it and all other cloneable related questions long before posting this question.

Most of the Java Collections Framework classes implement it, so classes like ArrayList, LinkedList, and HashMap.
As Jens points out in the comments, there's a "Use" link at the bottom of the Javadoc that takes you to a page full of uses of the Cloneable interface:
If you scroll down on that page, you'll see long lists of classes in various packages that implement Cloneable.

Related

Why only java.lang. Object is given superclass in java? [duplicate]

This question already has answers here:
Why Object class is Superclass in java [closed]
(4 answers)
Closed 6 years ago.
Is there any reason sun microsystems make Object for all javaclass for superclass. I face the question my last interview. I hope, I can find answers here
Thanks
Following could be the reasons for this design decision,
By having the Object as the super class of all Java classes, without knowing the type we can pass around objects using the Object declaration.
Before generics was introduced, imagine the state of heterogeneous Java collections. A collection class like ArrayList allows to store any type of classes. It was made possible only by Object class hierarchy.
The other reason would be to bring a common blueprint for all classes and have some list of functions same among them. I am referring to methods likehashCode(), clone(), toString() and methods for threading which is defined in Object class.
Please check the below link. I hope it will answer your question.
Why object is super class in JAVA

What do I win when I implement an interface which is blank? [duplicate]

This question already has answers here:
What is the purpose of a marker interface?
(10 answers)
Closed 7 years ago.
I have a question regarding "Interfaces" in Java and the question sounds like this:
What is the use of implementing a blank (empty) interface in my class?
In order to get a better understanding of the question, I will give you a concrete example:
If you go and see the implementation of "ArrayList" class, you will find out that it implements two interfaces (RandomAccess and Cloneable), which are actually totally empty!
Why is that happening?
What do I win by implementing a totally blank interface for my class?
In case you have any ideas, please leave a comment.
Thank you in advance.
Those interfaces are called as Marker interfaces (used to mark the class of that type) and at run time they are used to check the type.
For ex
While running the programm, internal logic may goes like
if (yourList instanceof Cloneable) {
// Hey this object is of type Clonable, please proceed
} else {
// Not that type. Reject
}
Those interfaces serve only for differing and identifying instances, consider this:
interface MyInterface1 {}
interface MyInterface2 {}
Consuming code:
if (foo is MyInterface1) ...

How marker interface are identified by JVM? [duplicate]

This question already has answers here:
How Marker Interface is handled by JVM
(7 answers)
Closed 7 years ago.
I have gone through few question in stack overflow but could not find a suitable answer. So raising it for more clarity.
I know a marker interface is an interface with no methods. When we implement a marker interface for example Serializable it declares that the class implementing it becomes eligible for serialization.
My question is how JVM understands that the objects of class implementing Serializable interface should be serialized. If i write an interface with no methods and hope that objects of class that implements it will be serialized i'll not work that way.
Is it possible for us to create a custom marker class.?
They aren't 'identified by the JVM' at all. They're identified by the Java code that is interested in them, for example ObjectOutputStream, via the instanceof operator.

What is use of user defined marker interface, and how it works? [duplicate]

This question already has answers here:
What is the use of marker interfaces in Java?
(10 answers)
Closed 8 years ago.
What is use of user defined marker interface, and how it works?
In case of already defined marker interfaces such as serializable or cloneable, the JVM do some internal processing, but for the user defined marker interface how JVM behave?
The Marker Interface pattern is a well known pattern that allows you to indicate something about a type without implementing any behaviour. Wikipedia does a better write-up that I can summarize so you should read that.
To answer your question directly, the JVM won't treat your type any different. It will be a type that implements an interface and that is it.

Java Cloneable or copy constructor, why would I use any of those? What do either of those strategies actually do? [duplicate]

This question already has answers here:
How do I copy an object in Java?
(23 answers)
Closed 9 years ago.
The question I have is pretty noob like so please excuse me for my ignorance as I am a noob.
I came across code some consultants wrote in the company I work for. When I tried delving into the code, I had no idea why a class was implementing some interface called clonable. So I tried to google this clonable mess, and all I see is stuff like "don't use it" or "use copy constructor instead". I don't know what either of those things are. Could someone please elaborate the reasons for when this kind of cloning is actually needed? Why would I clone an object?
I spoke to the ex-consultant and he mentioned this would allow us to chain methods apparently. Like questionSet.dosomething().doAnotherThing().dowth();
public class QuestionSet implements Cloneable {
...
/* (non-Javadoc)
* #see java.lang.Object#clone()
*/
#Override
public QuestionSet clone() {
return new QuestionSet(this);
}
...
}
It is used in situations where you pass an object to some other party. If the object is mutable and you do not want to risk that the other party changes your object you could give it just a clone or copy of your object instead of the original.

Categories