Runtime ClassCastException when assigning clone of Area subclass

Discussion in 'Java' started by Richard A. DeVenezia, Jan 8, 2004.

  1. 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.

    --
    Richard A. DeVenezia
    Richard A. DeVenezia, Jan 8, 2004
    #1
    1. Advertising

  2. Hi!

    Richard A. DeVenezia wrote:
    > 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
    Frank Birbacher, Jan 8, 2004
    #2
    1. Advertising

  3. "Richard A. DeVenezia" <> wrote in message
    news:btis87$7jarn$-berlin.de...
    | Help!

    People are less likely to help when
    you multi-post..
    Andrew Thompson, Jan 8, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    438
    jstorta
    Feb 20, 2006
  2. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    714
    lordy
    Jul 7, 2006
  3. S.Volkov
    Replies:
    2
    Views:
    213
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    320
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    394
Loading...

Share This Page