• Post Reply Bookmark Topic Watch Topic
  • New Topic

please help to debug this code  RSS feed

 
rizwana mujawar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output should be:

Before : Radhika 28
After : Radhika 28
But after the first output InvalidClassCastException is thrown.... How to handle this?
Thanks...!




 
Rose Ellis
Greenhorn
Posts: 16
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your superclass Animal needs a default constructor. The documentation of Serializable says the following:
"To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime."

also to be nitpicking, your expected results would be slightly different as your println() statements print the size before the name
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the full message of the Exception. It says no valid constructor. Aren’t you supposed to have a no-args constructor for the erdObject method? Look at this. You may be able to cure it by making Animal Serializable, too.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rose Ellis wrote:Your superclass Animal needs a default constructor.

It should also be noted that this constructor does NOT have to be public. In fact, if you hadn't planned on providing one, it shouldn't be.
The usual practise for a hierarchy is to make it protected.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!