Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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();
}

Java, Programming

  • Category:- Java
  • Reference No.:- M9547438

Have any Question?


Related Questions in Java

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As