473,473 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

overide clone() question

Hi All,

I am writing my own implementation of queue via a linked list, note
not a LinkedList, and was running into trouble with the clone method.
I was wondering if anyone could point out some not so obvious errors I
have made..

Cheers
public class QueueLinkedList implements Queue {
private QueueNode front; // note that ONLY front is used
private QueueNode rear; // for empty tests
private int numItems;

/* ************************************************** *****************
*/
/** create a queue
*
*/
public QueueLinkedList() {
front = null;
numItems = 0;
} // constructor QueueLinkedList

/** create a queue which can hold up to size elements
*
* @param max the size of the Queue to be created
* ie the maximum number elements it can hold
*/
public QueueLinkedList(int max) {
this(); // ignore max!
} // constructor QueueLinkedList

/* ************************************************** *****************
*/
/** Returns a string representation of the Queue <br> <br>
* pre: <br>
* post: a comma separated list of elements is returned starting
at front
*
* @return a string representation of the Queue consisting of a
comma
* element list starting with the element at the front of
the
* Queue
*/
public String toString() {
String s = "";
QueueNode node = front;

if (node != null) { // all except first has leading comma & no
(front)
s = "(FRONT) " + node.item;
node = node.link;
}
while (node != null) {
s = s + ", " + node.item;
node = node.link;
}
return s;
} // toString

/* ************************************************** *****************
*/
/** Removes all elements from this Queue <br> <br>
* this : [ TRUE, this = < > ]
*/
public void makeEmpty( ) {
front = null;
numItems = 0;
} // makeEmpty

/* ************************************************** *****************
*/
/** Determines whether this Queue is empty. <br> <br>
* : [TRUE, result = ( this = < > ) ]
*
* @return the boolean value 'this Queue is empty'
*/
public boolean isEmpty() {
return numItems==0;
// or return front==null or rear==null;
}

/* ************************************************** *****************
*/
/** Determines whether this Queue is full. <br> <br>
* : [ TRUE , result = ( #this = max) ]
*
* @return the boolean value 'this Queue is full'
*/
public boolean isFull() {
return false;
} // isFull

/* ************************************************** *****************
*/
/** Determines number of elements in this Queue. <br> <br>
* : [ TRUE , result = #this ]
*
* @return the number of elements in this Queue
*/
public int size() {
return numItems;
}

/* ************************************************** *****************
*/

/** Adds "element" to this Queue <br> <br>
* this : [ #this<max, fr this = this0 AND lt this = element]
*
* @param element the element to be added at the end of this
Queue.
*/
public void insert(Element item) {
QueueNode node = new QueueNode();

// if queue was empty, new node is also front
if (front == null) {
front = node;

// else add new node at rear
} else {
rear = node;
}
node.link = null; // not needed

// either way, it's the new rear
rear = node; // ???

// another logical step
node.item = item.copy();

// and another
numItems++;
}

/* ************************************************** *****************
*/
/** Removes and returns the element at the front of this Queue
<br> <br>
* this : [ #this > 0, tl this0 = this AND result = hd this0]
*
* @return the element which has been removed from the front of
this
* (previously) non-empty Queue.
*/
public Element remove(){
QueueNode node;

// remove the front
node = front;
front = null;

numItems--;

// if this made the queue empty, update rear too
node = rear.link
rear.link = node.link;
}

return rear.link;
}

/* ************************************************** ****************
*/
/** Returns the element at the front of this Queue without
removing it
* <br> <br>
* : [ #this > 0, result = hd this0]
*
* @return the element located at the front of this non-empty
Queue.
*/
public Element front( ) {
return rear.link.item.copy();
}

/* ************************************************** ****************
*/
/** Applies the "visit" method of "forAllObj" to all the elements
of this
* Queue <br> <br>
* this : [ TRUE, this = Vs:this0 . forAllObj.visit(s) ]
*/
public void forAll(ForAll forAllObj) {

if (front!=null) {
QueueNode node=front;
do {
forAllObj.visit(node.item);
node=node.link;
} while (node!=null);
}

} // forAll

/**
* Creates a new Queue having the same elements as this Queue.
<br>
* N.B. A new Queue is created, not just another reference to this
* Queue.<br>
* Overrides the protected Object.clone() method, so that
references
* outside the Object-SD2Set hierarchy can be used to call clone()
* public Object clone(). <p>
* , :[true, result=this] <br>
* pre: True. <br>
* post: A deep copy of this Queue is returned.
*
* @return A deep copy of this Queue.
*/
public Object clone() {
try {
QueueLinkedList clone = (QueueLinkedList)super.clone();

// clone the first node, if any
if (front!=null) {
clone.front = new QueueNode();
clone.front.item = (Element)front.item.clone();

// clone the rest of the queue contents, if any
QueueNode current = front.link;
QueueNode clonePrevious = clone.front;
while (current != null) {
clonePrevious.link = current.link;
clonePrevious = current;
clonePrevious.item = front.item;
current = front;
}
// XXXXXX
}

return clone;
} catch (CloneNotSupportedException cNSE) {
throw new Error("This can't happen");
}
}

}

class QueueNode

{

Element item;
QueueNode link;

/**
* Construct an empty QueueNode object
*/
public QueueNode() {
} // QueueNode

}

The Queue interface just says methods such as remove and insert must
be implemented, it also says public Object clone(); must be
implemented, which overides Object.clone()

The errors I get are:

QueueSD2LinkedList.java:2: The method java.lang.Object clone()
declared in class java.lang.Object cannot override the methode
signature declared in interface Queue. The access modifier is made
more restrictive.

also:

The method java.lang.Object clone() declared in class java.lang.Object
cannot override the method of the same signature declared in interface
Queue. Their throws clauses are incompatible.

Any light shed on this would be most helpfull
Jul 17 '05 #1
0 2650

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vincent | last post by:
Hey, I have a problem to understand the underlaying of clone method. Hope someone can help me out. Let's say, clonedObject = originalObject.clone() (syntax may be wrong, but you know what I...
1
by: Alfred Salton | last post by:
Can anyone show me a simple way to clone an object? In this case, I have an HTMLTable object which has a hidden header row acting as a template. I would like to clone the header row and add the new...
1
by: Jeff Haumesser | last post by:
I have a Collection called Grooming. Each object in this collection contains the property GroomingServices of type Collection. I also have a Clone of this Groomings collection called GroomingsInit....
4
by: csharpula csharp | last post by:
Hello, Have a basic question about Clone() If I am using the clone method on some object ,are the Cloned object properties going to change at every property change in original object? Thanks! ...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
1
by: woessner | last post by:
I have a lot of classes which derive from a common base class. I want to be able to clone objects of these types so I have given the base class a pure virtual clone method. The derived classes...
2
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I have a textBox on a windows form. Can i overide the textbox OnPaint directly in the form, if so how. Or do i have to inherit the textbox control and overide it there? Regards Martin
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.