File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Serialszing method of superclass which is non serilizable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Serialszing method of superclass which is non serilizable" Watch "Serialszing method of superclass which is non serilizable" New topic
Author

Serialszing method of superclass which is non serilizable

manisha makwana
Ranch Hand

Joined: Sep 14, 2007
Posts: 37
Hello,

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
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

manisha:I want to serialize all the method


Methods!!!
Methods are not serialized, it is the instance data that is serialized.
Do you wish to reframe your question?


apigee, a better way to API!
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Manisha,

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.

Marco
manisha makwana
Ranch Hand

Joined: Sep 14, 2007
Posts: 37
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.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

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.
manisha makwana
Ranch Hand

Joined: Sep 14, 2007
Posts: 37
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.

Example:

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialszing method of superclass which is non serilizable