Extending a class that implements Serializable
Asked Answered
M

2

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
Mikaela answered 15/11, 2017 at 20:27 Comment(1)
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?Mashe
H
6

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.

Hamman answered 15/11, 2017 at 20:32 Comment(0)
E
3

Per Javadoc:

All subtypes of a serializable class are themselves serializable

Edessa answered 15/11, 2017 at 20:30 Comment(10)
This is slightly misleading: all subtypes of a class implementing Serializable also implement Serializable; this says nothing about whether either actually can be serialized.Mashe
@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.Edessa
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.Mashe
For example: class A { class B implements Serializable {} }: instances of B aren't serializable, because of the implicit reference to non-serializable A.Mashe
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.Edessa
@Edessa you can attempt to serialize any object, whether or not it implements Serializable: ObjectOutputStream takes Object parameters, not Serializable.Mashe
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.Hamman
@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.Edessa
@Edessa 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.Mashe
And you've just restated my initial point: Serializable is not a guarantee of serializability.Mashe

© 2022 - 2024 — McMap. All rights reserved.