0

Suppose I have a java class, MyClass, that is not inherited from any class. MyClass implements Serializable.

Can MyClass be Serializable with no other conditions? Does it depend on what objects MyClass contain, and if these objects are Serializable themselves?

For example in the class below, if Mappedoodle2 does not implement Serializable, will I get an exception when Mappedoodle is serialized?

import java.io.Serializable;

public class Mappedoodle implements Serializable {
   private static final long serialVersionUID = -1760231235147491826L;
   private String text;
   private int value;
   private Mappedoodle2 mm = new Mappedoodle2();

   public Mappedoodle(String text, int value) {           
       this.text = text;
       this.value = value;
   }

   public String getText() {
       return text;
   }

   public int getValue() {
       return value;
   }

   @Override
   public String toString() {
       return text + ", " + value;
   }

}
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
yryrp
  • 37
  • 8
  • Alot of these questions are already answered. So I'll just refer you to it: http://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference – FirebladeDan Jun 19 '15 at 21:29
  • Remember that an interface is an agreement that you're making when using it that you will define the provided methods. – FirebladeDan Jun 19 '15 at 21:32
  • 1
    FirebladeDan : I'm not asking about differnt between inherits and implements. I asked if objects in Serializable class must them self implement Serializable interface – yryrp Jun 19 '15 at 21:41
  • Are you saying MyClass objects? or other Objects that can live in MyClass? This serializable idea is just a marker. You can serialize any object into bytes just need the marker.. – FirebladeDan Jun 19 '15 at 21:52
  • MyClass holds (contain) other objects : String , Integer , List and some other self defined objects (MyClass1 , MyClass2 ...). All these objects has to implements Serializable too (beside of MyClass ) ? – yryrp Jun 19 '15 at 21:56
  • Child classes do need to be serializable calling classes do not. – FirebladeDan Jun 19 '15 at 22:01
  • Yes you do! You must define `mm` transient, or make it class serializable. – Amir Pashazadeh Jun 19 '15 at 23:17

2 Answers2

1

To serialize the object, I'm assuming you are using an ObjectOutputStream. Here's some relevant documentation: http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html#writeObject(java.lang.Object)

That section describes the basics of how objects are written. The stream writes the following:

  • The class that the object is
  • The signature (public class Mappedoodle implements Serializable)
  • All fields that are neither transient nor static

Each field that is an object will also be written to the stream. This means that each field must implement Serializable. If it doesn't, you will get a NotSerializableException.

You have a couple of options for making this work:

  • Make Mappedoodle2 serializable
  • Make the field mm transient, which just means it won't be serialized.
James Westman
  • 2,680
  • 1
  • 15
  • 20
0

First, all classes technically extend from java.lang.Object, so even MyClass will inherit from it.

MyClass can implement Serializable just by implementing the interface, like any other interface. If a class contains properties of a class that implements Serializable, the parent class need not implement Serializable itself.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80