Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializable Inheritance

Tags:

c#

.net

vb.net

If something inherits from a Serializable class, is the child class still Serializable?

like image 356
Joe Morgan Avatar asked Oct 08 '08 13:10

Joe Morgan


People also ask

Is serializable 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.

Is serializable inherited Java?

In Java, an object is said to be serializable if its class or parent classes implement either the Serializable interface or the Externalizable interface. Deserialization is converting the serialized object back into a copy of the object.

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.

What is meant by serializable?

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java. io.


1 Answers

It depends what you mean be serializable. If you mean the CLI marker (i.e. the [Serializable] attribute), then this is not inherited (proof below). 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.

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(typeof(Foo).IsSerializable); // shows True
        Console.WriteLine(typeof(Bar).IsSerializable); // shows False
    }
}

[Serializable]
class Foo {}

class Bar : Foo {}
like image 90
Marc Gravell Avatar answered Oct 12 '22 14:10

Marc Gravell