Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A query about Serialization  RSS feed

 
Vishram Khare
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 527 of book--Beginning Java 2 JDK 5 edition by Ivor Horton, it is written that the class,the object of which you want to serialize has to fulfill the following conditions---

i) The class must be declared public.
ii) The class must implement interface Serializable.
iii)If the class has a direct or indirect superclass that is not serializable, then that class must have a default constructor.The derived class must take care of transferring the base class data members to the stream.

2nd and 3rd conditions are obvious and can be understood.

However is 1st condition necessary?Because I wrote a code that had a serializable class with default access in package (not public) and still the program compiled and ran well.

Further on page 528 of the same book,the author says if the class is not public,InvalidCastException might be thrown. In my code,the program did not throw any exception even though the class was not public.

Does anybody have any idea about it?

Thanks

----Vishram
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishram Khare:
i) The class must be declared public.


I didn't think there was any such requirement. Also, I just had a look at the Serialisation spec and didn't find anything that said the class had to be public.

Seems to me like your book is wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!