• Blogging
  • Freebies
  • Freeware
  • How-To
  • Make Money Online
  • Mobile
  • Home >> Java >> How to avoid serialization of sub-class of super class which implements Serializable interface?

    How to avoid serialization of sub-class of super class which implements Serializable interface?


    In Java, all sub-classes of super class which implements Serializable interfaces becomes serializable.

    What If you want to make sub-class non serialiazable ?

    It is possible to make a sub class non serializable.

    How to avoid serialization of sub class of super class which implements Serializable interface

    There is no such feature in java where you can make the sub-class unimplement an interface which is implemented by super class. So, that way you cannot make a sub class non serializable.

    How to avoid or remove serialization of sub-class of a super class which implemented Serializable interface?

    Implement writeObject() and readObject() methods in the sub-class and throw NotSerializableException from the method. By doing this you can avoid or stop serialization of the sub-class.

    
        class SerializableSuperClass implements Serializable {
    	...
        }
    
        class NonSerializableSubClass extends SerializableSuperClass {
        private void writeObject(java.io.ObjectOutputStream out)
             throws IOException {
         throw new NotSerializableException(“Can not serialize this class”);
        }
         private void readObject(java.io.ObjectInputStream in)
             throws IOException, ClassNotFoundException {
         throw new NotSerializableException(“Can not serialize this class”);
        }
         private void readObjectNoData()
             throws ObjectStreamException; {
         throw new NotSerializableException(“Can not serialize this class”);
        }
        }
    
    

    If you know any other way to do that, do share it

    IMPORTANT: This blog welcomes Guest Bloggers, Writers to also contribute by writing Guest Posts and also Make Money!. Check out our Revenue Sharing Program
    1924 readers are already subscribed to this blog! Why don't you be one of them? Subscribe to this blog via your favorite RSS feed reader or by entering your email address on the form below:





    Leave a Reply

    page counter
    NiharsWorld on Twitter NiharsWorld on Facebook NiharsWorld RSS Feed