Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending a class that implements Serializable

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
like image 709
Mars Avatar asked Nov 15 '17 20:11

Mars


People also ask

What happens if a class 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.

Can an interface extend Serializable?

Yes, you can extend the Serializable interface. If you do, all classes that implement the new subinterface will also be implementing Serializable .

What will happen if a class is Serializable but its superclass does not?

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.

Can Serializable class inherited?

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.

What are the implications of extending a serializable class?

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.

What is a Serializable interface in Java?

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.

When do we need to implement serialization in Entity Framework?

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.

When should an API class be serializable?

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?


Video Answer


2 Answers

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.

like image 170
John Bollinger Avatar answered Oct 17 '22 10:10

John Bollinger


Per Javadoc:

All subtypes of a serializable class are themselves serializable

like image 45
tsolakp Avatar answered Oct 17 '22 11:10

tsolakp