Interfaces
Test Cloneable
The program uses the Cloneable interface to mark classes cloneable and uses the clone method to copy objects. The program creates a new class named CloneableCircle that extends Circle and implements Cloeanbale interface. The main method creates a CloenableCircle object, and copies it to a new variable.
// TestCloneable.java: Cloning objects public class TestCloneable { /** Main method */ public static void main(String[] args) { // Declare and create an instance of CloneableCircle CloneableCircle c1 = new CloneableCircle(5); CloneableCircle c2 = (CloneableCircle)c1.clone(); System.out.println("After cloning c1 to c2"); // Check if two variables point to the same object System.out.println("Do c1 and c2 reference the same object? " + (c1 == c2)); // Check if the date field of two objects are the same System.out.println("Do c1.date and c2.date reference " + "the same object? " + (c1.date == c2.date)); System.out.println("Is a CloneableCircle object cloneable? " + (c1 instanceof Cloneable)); // Check if a Circle object is cloneable Circle c = new Circle(); System.out.println("Is a Circle object cloneable? " + (c instanceof Cloneable)); } } // CloneableCircle is a subclass of Circle, which implements the // Cloneable interface class CloneableCircle extends Circle implements Cloneable { // Store the date when the object is created java.util.Date date = new java.util.Date(); /** Construct a CloneableCircle with a specified radius */ public CloneableCircle(double radius) { super(radius); } /** Override the protected clone method defined in the Object class, and strengthen its accessibility */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { return null; } } }
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
so, the get method name is isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}