1

I am currently trying to learn and understand how to store data from user input as a file.

Currently, I have the classes "movies" and "series" and the class "MediaLibrary".

Now's the question: Which classes have to implement Serializable?

Do I only have to implement it at MediaLibrary (since I store objects from the other classes in ArrayList's) or do the other classes need Serializable too?

Thanks for your help.

LevKaz

EDIT 1: What makes my question unique from the possible duplicate? In the possible duplicate the guy asked, why not all objects are serializable, since he doesn't wanted to write it in every single class. I wanted to know (and understand) if it would be enough to serialize one class which is holding other objects, or if all objects have to implement the serializable interface.

Hopefully I showed up the difference of my question to the one I got suggested by Ali Seyedi.

LevKaz
  • 74
  • 1
  • 7
  • Possible duplicate of [Why Java needs Serializable interface?](http://stackoverflow.com/questions/441196/why-java-needs-serializable-interface) – Ali Seyedi Apr 14 '16 at 00:28

1 Answers1

3

If you want it to work by just writing implements Serializable, then all of the classes will need to be Serializable, because otherwise an exception will be thrown when attempting to serialize the ArrayList.

You could avoid making movies and series Serializable by writing writeObject in such a way that instead of writing movies and series objects to the ObjectOutputStream, you write their fields directly. However, you should not adopt such an approach unless you have very good reason to do so.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116