?
Solved

Cloneable Interface and Inner Classes - ClassCastException

Posted on 2005-03-21
9
Medium Priority
?
387 Views
Last Modified: 2010-03-31
I'm learning about interfaces, clone methods, and inner classes and have encountered a problem in a project.  First, we have a class that represents a person as an object. This implements the Cloneable interface.  The class Date is an inner class of Person and represents a date and includes a bunch of methods for formatting and such.  I was supposed to write clone() methods using the Cloneable interface and don't encounter any compile errors.  However, when I try to use the clone() method, a runtime error occurs.  It throws a ClassCastException at the point marked below.  I was wondering if anyone could help me find errors in the code or have ny helpful hints or information on the subject?

Here are the important bits and the constructors:

public class Person implements Cloneable {
  private String name;
  private Date born;
  private Date died;
  public Object clone() {
    try {
      Person copy = (Person)super.clone();
      copy.name = name;
      copy.born = (Date) born.clone();
      copy.died = (Date) died.clone();
      return copy;
    }
    catch (CloneNotSupportedException e) {
      return e;
    }
  }
  public Person() {}
  public Person(String initialName, Date birthDate, Date deathDate) {
    if (consistent(birthDate, deathDate)) {
      name = initialName;
      born = new Date(birthDate);
      if (deathDate == null)
        died = null;
      else
        died = new Date(deathDate);
    }
    else {
      System.out.println("Inconsistent dates. Aborting.");
      System.exit(0);
    }
  }
  public Person(Person original) {
    if (original == null) {
      System.out.println("Fatal error.");
      System.exit(0);
    }
    name = original.name;
    born = new Date(original.born);
    if (original.died == null)
      died = null;
    else
      died = new Date(original.died);
  }
****other methods****
********Inner Class:***   public class Date {
      private String month; //always 3 letters long, as in Jan, Feb, etc.
      private int day;
      private int year; //a four digit number.
      public Date() {
        month = "Jan";
        day = 1;
        year = 1000;
      }
   public Object clone() {
     try {
       Date copy = (Date)super.clone();
       copy.month = month;
       copy.day = day;
       copy.year = year;
       return copy;
     }
     catch (CloneNotSupportedException e) {
       return e;
     }
   }
   public Date(int monthInt, int day, int year) {
     setDate(monthInt, day, year);
   }
   public Date(String monthString, int day, int year) {
     setDate(monthString, day, year);
   }
   public Date(int year) {
     setDate(1, 1, year);
   }
   public Date(Date aDate) {
     if (aDate == null) { //Not a real date.
       System.out.println("Fatal Error.");
       System.exit(0);
     }
     month = aDate.month;
     day = aDate.day;
     year = aDate.year;
   }
***Other Methods***
}

-----------------------------------------------------------------------
Driver program:

public class PersonDemo {
  public static void main(String[] args) {
******Create a Person object so I can invoke the inner class Date:***    Person p = new Person();
    Person bach = new Person("Johann Sebastian Bach", p.new Date("Mar", 21, 1685),
                             p.new Date("Jul", 28, 1750));
    System.out.println(bach);

******Problem line:***  Person bachTwin = (Person)bach.clone();

******Working Alternative:***//  Person bachTwin = new Person(bach);

    System.out.println("Comparing bach and bachTwin:");
    if (bachTwin == bach)
      System.out.println("Same reference for both.");
    else
      System.out.println("Distinct copies.");
    if (bachTwin.equals(bach))
      System.out.println("Same data.");
    else
      System.out.println("Not same data.");
  }
}

Thank you for your time.  If you need additional code or information, please let me know.
0
Comment
Question by:rkjohnson2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13597722
Where is the CCE occurring?
0
 

Author Comment

by:rkjohnson2005
ID: 13597732
"Driver program:

public class PersonDemo {
  public static void main(String[] args) {
******Create a Person object so I can invoke the inner class Date:***    Person p = new Person();
    Person bach = new Person("Johann Sebastian Bach", p.new Date("Mar", 21, 1685),
                             p.new Date("Jul", 28, 1750));
    System.out.println(bach);

******Problem line:***  Person bachTwin = (Person)bach.clone();  <<<<<<<<<<<<<<<<HERE IS WHERE THE EXCEPTION IS THROWN

******Working Alternative:***//  Person bachTwin = new Person(bach);" <<<<<<<<<<<<THIS LINE CAUSES NO PROBLEMS (AS FAR AS I CAN TELL) BUT DOESNT USE THE METHODS REQUIRED
0
 
LVL 92

Expert Comment

by:objects
ID: 13597739
can you post the stack trace for the CCE please.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rkjohnson2005
ID: 13597745
I'm not completely sure what the stack trace is - Do you mean this?

java.lang.ClassCastException
      at project_9_.Person.clone(Person.java:36)
      at project_9_.PersonDemo.main(PersonDemo.java:31)
Exception in thread "main"
0
 
LVL 92

Expert Comment

by:objects
ID: 13597750
>   catch (CloneNotSupportedException e) {
>      return e;
>    }

you shouldn't be returning e, if an exception occurs.
Probably should rethrow an appropriate exception
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13597758
Also your Date class does not implement CLoneable (which is probaly the case of the problem)
0
 

Author Comment

by:rkjohnson2005
ID: 13597765
public class Person {
***
  public Object clone() {
    try {
      Person copy = (Person)super.clone();
      copy.name = name;
>>>>>>>>>LINE 36:      copy.born = (Date) born.clone();
      copy.died = (Date) died.clone();
      return copy;
    }
    catch (CloneNotSupportedException e) {
      return e;
    }
  }

public class PersonDemo {
  public static void main(String[] args) {
    Person p = new Person();
    Person bach = new Person("Johann Sebastian Bach", p.new Date("Mar", 21, 1685),
                             p.new Date("Jul", 28, 1750));
    System.out.println(bach);

LINE 31: >>>>>>>>>>   Person bachTwin = (Person)bach.clone();
0
 

Author Comment

by:rkjohnson2005
ID: 13597774
OMG.  That was it.  Date didn't implement Cloneable. Such a simple solution. . . I'm a moron. Thank you so much!
0
 
LVL 92

Expert Comment

by:objects
ID: 13597785
no worries :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question