Java Reference
In-Depth Information
public class IntegerStack implements Cloneable { //
dangerous
private int[] buffer;
private int top;
public IntegerStack(int maxContents) {
buffer = new int[maxContents];
top = -1;
}
public void push(int val) {
buffer[++top] = val;
}
public int pop() {
return buffer[top--];
}
public IntegerStack clone() {
try {
return (IntegerStack) super.clone();
} catch (CloneNotSupportedException e) {
// Cannot happen -- we support clone
throw new InternalError(e.toString());
}
}
// ...
}
Here we override clone to make it public, but we use the default imple-
mentation from the Object class.
Now let's look at some code that creates an IntegerStack object, puts
some data onto the stack, and then clones it:
IntegerStack first = new IntegerStack(2);
first.push(2);
 
Search WWH ::




Custom Search