Ness Interview Question
Software Engineer / DevelopersBut JVM will look into readObject(OutputStream out) and writeObject(InputStream in) methods only if class is implementing Externizible right? Here it is Serializable interface?
http:java.sun.com developer technicalArticles Programming serialization
Stop That Serialization!
OK, we have seen quite a bit about the serialization process, now let's see some more. What if you create a class whose superclass is serializable but you do not want that new class to be serializable? You cannot unimplement an interface, so if your superclass does implement Serializable, your new class implements it, too (assuming both rules listed above are met). To stop the automatic serialization, you can once again use the private methods to just throw the NotSerializableException. Here is how that would be done:
10 private void writeObject(ObjectOutputStream out) throws IOException
20 {
30 throw new NotSerializableException("Not today!");
40 }
50 private void readObject(ObjectInputStream in) throws IOException
60 {
70 throw new NotSerializableException("Not today!");
80 }
Any attempt to write or read that object will now always result in the exception being thrown. Remember, since those methods are declared private, nobody could modify your code without the source code available to them -- no overriding of those methods would be allowed by Java.
In ur B class, implement the readObject(OutputStream out) and writeObject(InputStream in) methods.
- Anand January 30, 2011If a class contains these methods, then during Serialization the JVM calls them to do the serialization process.
In this methods, simply throw a NotSerializable Exception.