This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a superclass which is abstract and have some method which is final. Now my subclass extend this class.I want to serialize all the method of superclass and subclass how can i do that.According to me this is not possible but i just came to know this is possible please help me for this
methods aren't serializable. Just the state of an object gets serialized which means it's data or member variables.
The code of your methods stays the same for every new object and during its lifetime so it doesn't make sense to serialize and deserialize it.
Joined: Sep 14, 2007
Sorry for framing question in wrong way i want to serialize the state of object so how can i do that for superclass whose code is not with me i have only .class file and it is not even implementing serialize interface.
Originally posted by manisha makwana: Sorry for framing question in wrong way i want to serialize the state of object so how can i do that for superclass whose code is not with me i have only .class file and it is not even implementing serialize interface.
If you want to serialize the object of a superclass which doesn't implemet serializable then the simple answer is : NO you can't do it.
What happens is as the superclas is non-serializable so when and you deserialize the instance variables you inherit from the superclss will be set to default because the non-serializable superclass constructor will run.
However, you can try and extend the non-serializable class and implement your new class as serializable, and use this newly made class for your purpose.
Hope that helps.
Joined: Sep 14, 2007
Yes i know the answer is know but there is some indirect mechanism through which we can do that.So i want that mechanism if any one knows about it then please let me know.
You can serialize an object of a class that has a non-serializable super class, as long as that super class has a constructor without arguments. During de-serialization, instead of deserializing that super class' fields, its non-argument constructor is called.
This prints 19 and 5, not 13 and 5.
Now what you can do, if you have access to the fields, is create two methods, readObject and writeObject:
If you don't have access to them, you're out of luck though (unless you can use Reflection).
As others have mentioned, you can serialise Serializable subclass of a non-Serializable superclass.
If the superclass state is not required to be included in the serialisation, there is no problem. If the superclass state is required in the serialisation, you must resort to the unpleasant methods mentioned. Using direct field access or, if that's not possible, naughty reflection to bypass access controls, you will probably manage to save and load the superclass state.
HOWEVER, in many cases, if a class is non-Serializable, that is for a good reason: because it is inappropriate to try to store and reload an object of that class. Certainly, for most built-in Java API classes this is true: you are strongly advised not to try to serialise non-Serializable API classes.
An example would be a java.lang.Process object. Trying to store and reload such an object would be totally inappropriate, even if you were somehow able to achieve it.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Serialszing method of superclass which is non serilizable