7

If I extend a class that implements Serializable, do I need that class to also implement Serializable?

For instance if I have,

public class classToBeExtended implements Serializable

Then will this suffice?

public class classThatWillExtend extends classToExtended

Or do I need to do this?

public class classThatWillExtend extends classToExtended implements Serializable
Mars
  • 4,677
  • 8
  • 43
  • 65
  • If you extend a class that implements `List`, do you need to declare again that the subclass is also a `List`? Why would it be any different for `Serializable`? – Andy Turner Nov 15 '17 at 20:29

2 Answers2

5

If any of a class's superclasses implements a given interface, then the subclass also implements that interface. Serializable is not special in that regard, so no, the subclasses of a Serializable class do not need to explicitly declare that they implement Serializable. They can so declare, but doing that makes no difference.

The other implication is that if you extend a Serializable class, you should ensure that the subclass is indeed serializable itself. For example, don't add non-transient fields of non-serializable types unless you're prepared also to add the necessary methods to support them.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
3

Per Javadoc:

All subtypes of a serializable class are themselves serializable

tsolakp
  • 5,858
  • 1
  • 22
  • 28
  • 1
    This is slightly misleading: all subtypes of a class implementing `Serializable` also implement `Serializable`; this says nothing about whether either actually can be serialized. – Andy Turner Nov 15 '17 at 20:34
  • @Andy Turner. Not sure what you mean. From the Javadoc statement it should mean that sub classes are serializable without the need to implement the interface. At least that is how I am reading it. – tsolakp Nov 15 '17 at 20:37
  • 2
    implementing `Serializable` is a necessary but not sufficient condition for *being* serializable. You can add fields of non-serializable type to a class implementing `Serializable`, and if these are non-null (and the runtime type of the reference isn't serializable) then serialization will fail. – Andy Turner Nov 15 '17 at 20:42
  • 1
    For example: `class A { class B implements Serializable {} }`: instances of `B` aren't serializable, because of the implicit reference to non-serializable `A`. – Andy Turner Nov 15 '17 at 20:43
  • Sure thing. But the point of the statement is that Java runtime will attempt to serialize subclass. Whether or not it will success is another issue. – tsolakp Nov 15 '17 at 20:45
  • @tsolakp you can attempt to serialize any object, whether or not it implements `Serializable`: `ObjectOutputStream` takes `Object` parameters, not `Serializable`. – Andy Turner Nov 15 '17 at 20:47
  • And that is why it is important to distinguish between implementing `Serializable` and actually *being* serializable, @tsolakp, which the javadoc comment you quoted fails to do, which is what makes it misleading. – John Bollinger Nov 15 '17 at 20:48
  • @AndyTurner. Java wont try to serialize the object state unless it implements `Serializable`. What code will try to do and what the runtime will attempt to do are two different things. I dont think Java can guarantee that object that is instance of `Serializable` is 100% serializeable. The only guarantee for it to assume it is serializeable, is the marker `Serializable` interface itself. – tsolakp Nov 15 '17 at 20:57
  • @tsolakp I'm not sure what you mean by "Java won't try": of course it does. It tries, and that's when it fails with `NotSerializableException` if the runtime value of the reference refers to a non-serializable object. If it didn't try to do something, the exception wouldn't occur. – Andy Turner Nov 15 '17 at 21:10
  • And you've just restated my initial point: `Serializable` is not a guarantee of serializability. – Andy Turner Nov 15 '17 at 21:15