Runtime ClassCastException when assigning clone of Area subclass

  • Thread starter Richard A. DeVenezia
  • Start date
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.
 
F

Frank Birbacher

Hi!
Help! I can't seem to use a cloned instance of a subclass.
Might have something to do with public nature of Area.clone ? [snip]
public class FoldableArea extends java.awt.geom.Area {

boolean isFlipped = false;
Line2D.Double hinge;
[snip]
public Object clone () {
return super.clone();
}

Here you are creating a new instance of java.awt.geom.Area.

[snip]
FoldableArea a1 = (FoldableArea) area.clone(); // runtime error
occurs here

And here you are trying to cast the java.awt.geom.Area to FoldableArea,
which is not possible.

You have to fully implement the clone method yourself. The most
important instruction is "new FoldableArea()":

class FoldableArea ... {
public Object clone() {
FoldableArea fa = new FoldableArea();
fa.isFlipped = isFlipped;
fa.hinge = hinge.clone();
//..copy other things
return fa;
}
//...
}

Notice that there is no call to super, because it won't help much. Do
all copying yourself as nesscesary.

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top