R
Richard A. DeVenezia
Help! I can't seem to use a cloned instance of a subclass.
Might have something to do with public nature of Area.clone ?
Exception in thread "main" java.lang.ClassCastException
at Areas.fold(Areas.java:14)
at Foo.<init>(Foo.java:7)
at Foo.main(Foo.java:10)
Here is my source:
FoldableArea.java
----------
import java.awt.Shape;
import java.awt.geom.*;
public class FoldableArea extends java.awt.geom.Area {
boolean isFlipped = false;
Line2D.Double hinge;
public FoldableArea (Shape s) {
super (s);
}
public Object clone () {
return super.clone();
}
public void fold (Line2D.Double line) {
hinge = line;
// stuff
isFlipped = true;
}
}
----------
Areas.java
----------
import java.util.*;
public class Areas {
Vector areas;
public Areas() {
areas = new Vector();
}
public void add (FoldableArea a) {
areas.addElement (a);
}
public void fold () {
for (int i=0;i<areas.size();i++) {
FoldableArea area = (FoldableArea) areas.elementAt(i);
FoldableArea a1 = (FoldableArea) area.clone(); // runtime error
occurs here
FoldableArea a2 = (FoldableArea) area.clone();
}
}
}
----------
Foo.java
----------
import java.awt.geom.*;
public class Foo {
public Foo() {
Areas areas = new Areas();
areas.add (new FoldableArea (new Rectangle2D.Double (0,0,10,10)));
areas.fold();
}
public static void main(String args[]) {
new Foo();
}
}
----------
When FoldableArea is replaced with Area, there is no error.
I also tried having 'implements Cloneable', but error still happened.
I also tried return (FoldableArea) (super.clone);
I am subclassing Area so I can store the associated hinge information with
the area.
Might have something to do with public nature of Area.clone ?
Exception in thread "main" java.lang.ClassCastException
at Areas.fold(Areas.java:14)
at Foo.<init>(Foo.java:7)
at Foo.main(Foo.java:10)
Here is my source:
FoldableArea.java
----------
import java.awt.Shape;
import java.awt.geom.*;
public class FoldableArea extends java.awt.geom.Area {
boolean isFlipped = false;
Line2D.Double hinge;
public FoldableArea (Shape s) {
super (s);
}
public Object clone () {
return super.clone();
}
public void fold (Line2D.Double line) {
hinge = line;
// stuff
isFlipped = true;
}
}
----------
Areas.java
----------
import java.util.*;
public class Areas {
Vector areas;
public Areas() {
areas = new Vector();
}
public void add (FoldableArea a) {
areas.addElement (a);
}
public void fold () {
for (int i=0;i<areas.size();i++) {
FoldableArea area = (FoldableArea) areas.elementAt(i);
FoldableArea a1 = (FoldableArea) area.clone(); // runtime error
occurs here
FoldableArea a2 = (FoldableArea) area.clone();
}
}
}
----------
Foo.java
----------
import java.awt.geom.*;
public class Foo {
public Foo() {
Areas areas = new Areas();
areas.add (new FoldableArea (new Rectangle2D.Double (0,0,10,10)));
areas.fold();
}
public static void main(String args[]) {
new Foo();
}
}
----------
When FoldableArea is replaced with Area, there is no error.
I also tried having 'implements Cloneable', but error still happened.
I also tried return (FoldableArea) (super.clone);
I am subclassing Area so I can store the associated hinge information with
the area.