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

Explain data updates issues in data management, Explain Data Updates issues...

Explain Data Updates issues in data management? DATA UPDATES: Old titles must be removed regularly Data changes every time Prices change Transportation / shipping cost change

Myfirstprogram in java, The purpose of this assignment is to help you learn...

The purpose of this assignment is to help you learn the Java environment and practice I/O instructions, assignment, and simple arithmetic operators. Teams: The assignment can

Using rmi build robust distributed systems-jdbc odbc, This work assesses th...

This work assesses the following Outcomes: • Build robust, secure distributed systems using advanced programming techniques Use RMI to build a distributed application accessing

Decsion tree, implementation of DT in netbeans

implementation of DT in netbeans

Explain drawing rectangles in java, Explain Drawing Rectangles in java ? ...

Explain Drawing Rectangles in java ? Drawing rectangles is easy. Begin with a Graphics object g and call its drawRect() method: public void drawRect(int x, int y, int width, i

Assignment , hi i wanted to know if your tutors provide solutions for progr...

hi i wanted to know if your tutors provide solutions for programming assignments

Object and Instance, What is the difference between instance and object of ...

What is the difference between instance and object of a class? Few says both are same, then why java kept both the words for same thing?

What is constructor, What is Constructor? A constructor is used to init...

What is Constructor? A constructor is used to initialize a newly created object. It's called just after memory is allocated for the object. It can be used to initial

What is application context module, The Application context module makes sp...

The Application context module makes spring a framework. This module extends the concept of BeanFactory, giving support for internationalization (I18N) messages, application lifecy

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