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
If a super class implements Serializable, then its sub classes do automatically. When an instance of a serializable class is deserialized, the constructor doesn't run. If a super class doesn't implement Serializable, then when a subclass object is deserialized, the super class constructor will run.
Yes, you can extend the Serializable interface. If you do, all classes that implement the new subinterface will also be implementing Serializable .
Case 2(a): What happens when a class is serializable, but its superclass is not? Serialization: At the time of serialization, if any instance variable inherits from the non-serializable superclass, then JVM ignores the original value of that instance variable and saves the default value to the file.
You must explicitly mark each derived class as [Serializable] . If, however, you mean the ISerializable interface, then yes: interface implementations are inherited, but you need to be careful - for example by using a virtual method so that derived classes can contribute their data to the serialization.
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. Show activity on this post.
The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods. Classes implement it if they want their instances to be Serialized or Deserialized.
If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface. In practice, if our object is to leave the domain of the JVM, it'll require serialization. Each entity class consists of persistent fields and properties.
If the class represents a model object that may be transferred across the n/w, it should be serializable. Is this reasoning correct? If so, what is the logic behind some of native Java API classes being serializable while others are not?
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.
Per Javadoc:
All subtypes of a serializable class are themselves serializable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With