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

Logical operators, Logical Operators Operator ...

Logical Operators Operator Functionality E x ample/ Explanation     && Looks at 2 expressions & returns a

Compile, If any of the above conditions does not met, then the program will...

If any of the above conditions does not met, then the program will generate compilation errors. Today Rahul has written his first program "Hello World", but he is not sure about th

Help, Are you real? I dont want to get help from a computer

Are you real? I dont want to get help from a computer

Overloading method, QUESTION 3: Overloaded methods Write the overloaded me...

QUESTION 3: Overloaded methods Write the overloaded method named average () for each of the following problems: a) The first method receives THREE (3) integer values and returns

Mention five applications of artificial intelligent system, Mention any fiv...

Mention any five applications of Artificial Intelligent System? 1)Medical Diagnosis 2)Robots 3) Games 4) Business intelligence 5)Image Recognition.

Explain what is orm, Object Relational Mapping , its a tool for transaction...

Object Relational Mapping , its a tool for transaction management that requires to be integrated with Spring, Struts etc.Eg : Hibernate, i Batis,JDO etc

I want an idea for final year projects, i m confused what to take as a pro...

i m confused what to take as a project for final year in information tech cn u suggest some of the topic of software

Explain how java allows the constraints on a generic type, Consider the fol...

Consider the following C++ template class. #include using namespace std; template class SortedList { public: SortedList() {size = 0;} void insert(T item); friend ostrea

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