• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialszing method of superclass which is non serilizable

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic