I have 2 classes that implement the same interface and one of them also implements the Serializable interface. Now, when I try to serialize the class to the HD I get an exception (java.io.NotSerializableException). I'm 99% sure that the class is "not serializable" due to the fact that the super class (the interface) is not serializable.
If I make the interface extend Serializable the whole thing runs perfectly smooth. Here's my problem though. I don't want to make the interface Serializable because other classes implement that same interface and I don't need or want them to be Serializable. How can I make my class Serializable without making it's inherited interface Serializable as well?
Also, I thought about doing custom serialization but the interface the 2 classes implement has absolutely no data, so there is nothing extra to serialize. It's just used as a tag so that any classes that implement it can all be added to the same collection. (passes the IS-A test).
I'm pretty sure this is considered an easy question. Any help is appreciated, and I hope that all made since. I would have posted code but there is a lot of it. Well, I guess I could make some mock code if no one understands my explanation.
If you don't want the other interface to extend Serializable: don't. You need to go through the class to see whether there are any fields which are reference types which don't themselves implement Serializable. If there are, you have three options:
Make them implement Serializable
Label them "static"
Label them "transient"
You may not be able to implement any of those options. Fields labelled static or transient are not included when an object is serialised. You may have problems with transient fields; those need to be reinitialised when the object is de-serialised. The transient keyword is intended for things like passwords which ought to be deleted when the object is serialised.
If the class itself doesn't implement Serializable, you can always add "implements Serializable" to the class heading. You can't serialise the objects otherwise. If a class implements Serializable (or any interface), remember all its subclasses will implement Serializable too.
"go through the class to see whether there are any fields which are reference types which don't themselves implement Serializable"
I had forgotten that one of the objects I was serializing had another object reference in it that was not marked Serializable. The problem had nothing to do with the super class. It's fixed and working now.
For your reference, having a non-serializable super class is only a problem if that class does not have a constructor without parameters.
As for custom serialization, there are three methods you can use for that:
Usually you only need the first two. The third is mostly used in Singleton classes to make sure you can't create a copy by serializing and then de-serializing. By returning the sole singleton instance from this method the sole singleton instance is also returned when reading an object from an ObjectInputStream.