Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Serializable question in mock exam

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have doubt about following question.


Here in this question class does not implement Serializable interface and still compiles and runs fine. [it gives compile time error on readObject() is fine. We can typecast it to String[].]

But how does it serialize object without implementing inteface?

And in another question it asks to implement Serializable interface for the class.

The second one is given below:



why there is difference between two?

Any help is appreciated .

-Vivian
[ July 29, 2007: Message edited by: Vivian Josh ]
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vivian !



in the above code you are not serializing a ClassState object but you are serializing a String object,
and the String class does implement Serializable interface.

You should keep in mind that it the class whose object you want to serialize must implement
Serializable interface and NOT the class in which you are writing the code of Serialization..
In the above code your serialization procedure is written in ClassState which need not implement Serializable.

But in the second code...



you are serializing the object of class MyClass, hence MyClass need to implement the Serializable interface. And also since the whole serialization process is written in a different class SaveState, this class
need not implement Serializable interface unless you decide to serialize it's object as well...
[ July 29, 2007: Message edited by: Priyam Srivastava ]
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Priyam for detail explanation !
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic