File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Serializable exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Serializable exception " Watch "Serializable exception " New topic
Author

Serializable exception

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

hi Folks,

I was going through a nice question and it was that...Suppose super class of a new class implement Serializable interface, how can you avoid new class to being serialized?

TH/he answer to this solution is ..as per my understandings...If Super Class of a Class already implements Serializable interface in Java then its already serializable in Java, since you can not unimplemented an interface its not really possible to make it Non Serializable class but yes there is a way to avoid serialization of new class. To avoid java serialization you need to implement writeObject () and readObject () method in your Class and need to throw NotSerializableException from those method.

Now could you guys please show me through a small program demo,,!!that will be a great help...!! thanks in advance..!!


Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Saral Saxena wrote:
Now could you guys please show me through a small program demo,,!!that will be a great help...!! thanks in advance..!!


How about you writing it?


Mohamed Sanaulla | My Blog
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi ,

Please if you could provide me a short demo , I can then grasp from it, THANKS in advance..!!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

NO. YOU write it. You clearly already have all the information you need available. And if you get stuck while trying to write it, google for examples. And, after you spend at least an hour trying to write it yourself and trying to figure out your problems by searching on your own, if you're still stuck, then post a question here, showing what you've tried, and providing details about what exactly you're having trouble with.

Why is it necessary to tell you this in almost every thread you post?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Saral Saxena wrote:Now could you guys please show me through a small program demo,,!!

Saral, if you are serious about learning to program in Java, you will need to take initiative yourself, instead of constantly asking other people to write examples for you.

The best way to learn programming is to experiment yourself. Write code yourself, if it doesn't work, carefully try to understand what the error messages mean, change your program and try again, until it works.

It's also much more fun when you do that - the satisfaction of solving a problem you have been working on for a while is nice.

Now, about this question. You know already exactly what has to be done, because you wrote this yourself:
Saral Saxena wrote:To avoid java serialization you need to implement writeObject () and readObject () method in your Class and need to throw NotSerializableException from those method.

It's very easy to make a short program with exactly this. The API documentation of the interface java.io.Serializable (<= link, click on it) tells you exactly what the writeObject() and readObject() methods should look like.

Try it yourself, and post your results, then we'll tell you if you did it correctly.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Folks,

This is the piece of code which finally solves the things...



and there is a subclass...




Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

That's right!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serializable exception