How to implementing the cloneable interface, JAVA Programming

Assignment Help:

How to Implementing the Cloneable Interface

The java.lang.Object class contains a clone() method which returns a bitwise copy of the current object.
protected native Object clone() throws CloneNotSupportedException
Not all objects are cloneable. It particular only examples of classes that implement the Cloneable interface can be cloned. Trying to clone an object in which does not implement the Cloneable interface throws a CloneNotSupportedException.
For instance, to make the Car class cloneable, you simply declare that it implements the Cloneable interface. Because this is only a marker interface, you do not requires to add any methods to the class.

public class Car extends MotorVehicle implements Cloneable {

// ...

}

For example

Car c1 = new Car("New York A12 345", 150.0);
Car c2 = (Car) c1.clone();

Most classes in the class library do not implement Cloneable so their examples are not cloneable.

Most of the time, clones are shallow copies. Instead if the object being cloned holds a reference to another object A, then the clone holds a reference to the same object A, not to a clone of A. If this isn't the behavior you need, you can override clone() yourself.
You may also override clone() if you want to make a subclass uncloneable, while one of its superclasses does implement Cloneable. In this case simply use a clone() method in which throws a CloneNotSupportedException. For example,

public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Can't clone a SlowCar");
}
You may also need to override clone() to make it public instead of protected. In this case, you can simply fall back on the superclass implementation. For instance,

public Object clone() throws CloneNotSupportedException {
return super.clone();
}


Related Discussions:- How to implementing the cloneable interface

Inner, #question.Hi, i hope my message find you very well. could you help m...

#question.Hi, i hope my message find you very well. could you help me please with this question . provide an example code for an anonymous inner class. Highlight what makes your e

Create a new project in eclipse , Task 1 Create a new project in Eclips...

Task 1 Create a new project in Eclipse called Assignment 1. Within this project create a package called task01. 1/ Download the class Date (you must use this class - no

Illustrate object-to-relational mapping?, O/R mapping is well suited for re...

O/R mapping is well suited for read, change, write centric applications and not suited for write centric applications where data is seldom read. Although this was usually true of m

Explain how many ways businesses monitor their employees, Explain how many ...

Explain how many ways businesses monitor their employees? • Systems are available which monitor almost every key stroke that an employee forms on a computer. • Systems are avai

Create a usercontrol called loginpasswordusercontrol, (Login User Control) ...

(Login User Control) Create a UserControl called LoginPasswordUserControl that contains a Label (loginLabel) that displays string "Login:", a TextBox (loginTextBox), where the user

Develop a 3d rolling alarm clock, Need to develop a 3D rolling alarm clock ...

Need to develop a 3D rolling alarm clock (Android) Project Description: I want an android app as 3D rolling alarm clock It should have sound effect when setting the alarm

Differentiate between time slicing and preemptive scheduling, What is diffe...

What is difference between time slicing and preemptive scheduling? Differences between preemptive and time scheduling are: In case of Preemptive scheduling the highest prior

Compare classes of java with c++, Compare classes with C++ Some signif...

Compare classes with C++ Some significant points to consider when defining a class in java as you probably noticed from above given skeleton are -  There  are  no  global

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd