Can subclass avoid Serialization if its superClass has implemented Serialization interface in java




If superClass has implemented Serializable that means subclass is also Serializable (as subclass always inherits all features from its parent class), for avoiding Serialization in sub-class we can define writeObject() method and throw NotSerializableException() from there as done below.



private void writeObject(ObjectOutputStream os) throws NotSerializableException {
    throw new NotSerializableException("This class cannot be Serialized");
}
  


Full Program/SourceCode to show how subclass can avoid Serialization if its superClass has implemented Serialization interface>
package SerDeser11throwNotSerExc;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
/*Author : AnkitMittal  Copyright- contents must not be reproduced in any form*/
class Super implements Serializable{
   private static final long serialVersionUID = 1L;
}
class Sub extends Super {
  
   private static final long serialVersionUID = 1L;
   private Integer id;
  
   public Sub(Integer id) {
          this.id = id;
   }
   @Override
   public String toString() {
          return "Employee [id=" + id + "]";
   }
  
   /*
   * define how Serialization process will write objects.  
   */
   private void writeObject(ObjectOutputStream os) throws NotSerializableException {
          throw new NotSerializableException("This class cannot be Serialized");
   }
  
}
public class SerializeDeserialize {
   public static void main(String[] args) {
          Sub object1 = new Sub(8);
          try {
                 OutputStream fout = new FileOutputStream("ser.txt");
                 ObjectOutput oout = new ObjectOutputStream(fout);
                 System.out.println("Serialization process has started, serializing objects...");
                 oout.writeObject(object1);
                 fout.close();
        oout.close();
        System.out.println("Object Serialization completed.");
                
         
          } catch (IOException  e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
Serialization process has started, serializing objects...
java.io.NotSerializableException: This class cannot be Serialized
   at SerDeser11throwNotSerExc.Sub.writeObject(SerializeConstructorCheck.java:35)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
   at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
   at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
   at java.io.ObjectOutputStream.writeObject0(Unknown Source)
   at java.io.ObjectOutputStream.writeObject(Unknown Source)
   at SerDeser11throwNotSerExc.SerializeConstructorCheck.main(SerializeConstructorCheck.java:51)
*/
If we note output, subclass was Serializable (as subclass always inherits all features from its parent class), for avoiding Serialization in sub-class we defined writeObject() method and throwed NotSerializableException() from there.



RELATED LINKS>

Serialization And Deserialization Tutorial


Serializing and DeSerializing Singleton >

More about Serialization >






Interviews >


eEdit
Must read for you :