Monday, April 18, 2011

Core Java Interview Questions - Serialization

The following are some questions you might encounter with respect to Serialization in any Java Interview. Serialization is a powerful and useful concept that is widely used in many enterprise class Java Applications.

Apart from the questions below, there is an article that I have put up (as part of the SCJP Certification series) on Serialization that you might find useful. You can use it to revise/review your understanding of Servlets.

It is:

Serialization

Questions:


1. Explain the usage of the keyword transient?

The transient keyword indicates that the value of this variable need not be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (ex: 0 for integers).

2. What are the uses of Serialization?

Serialization is widely used to:

* To persist data for future use.
* To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
* To "flatten" an object into array of bytes in memory.
* To exchange data between applets and servlets.
* To store user session in Web applications.
* To activate/passivate enterprise java beans.
* To send objects between the servers in a cluster.

3. What is serialization ?

Serialization is the process of writing the complete state of java object into an output stream. This stream can be file or byte array or stream associated with a TCP/IP socket.

4. What does the Serializable interface do ?

Serializable is a tagging interface which declares/describes no methods. It is just used to signify the fact that, the current class can be serialized. ObjectOutputStream serializes only those objects which implement this interface.

5. How do I serialize an object to a file ?

To serialize an object into a stream perform the following steps:

1. Open one of the output streams, for exaample FileOutputStream
2. Chain it with the ObjectOutputStream - Call the method writeObject() providing the instance of a Serializable object as an argument.
3. Close the streams

6. How do I deserilaize an Object?

To deserialize an object, perform the following steps:

1. Open an input stream
2. Chain it with the ObjectInputStream - Call the method readObject() and cast tthe returned object to the class that is being deserialized.
3. Close the streams

7. What is Externalizable Interface ?

Externalizable interface is a subclass of Serializable. Java provides Externalizable interface so as to give you more control over what is being serialized and what is not. Using this interface, you can Serialize only the fields of the class you want serialize and ignore the rest.

This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same sequence.

8. What interface must an object implement before it can be written to a stream as an object?

An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.

9. What are the rules of serialization

Some rules of Serialization are:

1. Static fileds are not serialized because they are not part of any one particular object
2. Fileds from the base class are handled only if the parent class itself is serializable
3. Transient fileds are not serialized

If you have any questions that you want answer for - please leave a comment on this page and I will answer them.

If you have any more questions on Serialization that you have faced during your interviews and wish to add them to this collection - pls drop a note to anandvijayakumar007@gmail.com and I shall be glad to add them to this list.

More Java & J2EE Interview Questions with Answers - Click Here

4 comments:

  1. Hi

    I read this post 2 times. It is very useful.

    Pls try to keep posting.

    Let me show other source that may be good for community.

    Source: financial controller interview questions

    Best regards
    Jonathan.

    ReplyDelete
  2. I have the following questions about Serialization:

    i. Why the Object class doesn't implement Serializable interface? What was the harm in it?

    ii. What is SerialVersionUID? What is the significance and use of this?

    iii. If we serialize an object, and then add/delete some variables, then can we deserialize the object?

    ReplyDelete
  3. @ VaibhavG
    1. If the Object class is made Serializable it will add a lot of override while creating every single object you use in your programs. Less than 2% of objects we create for practical purposes are required to be serialized. So, making the remaining 98% stuff serializable is an overhead. isnt it?

    2. It is a unique id used to identify each object

    3. First of all, you cannot add/delete stuff from the serialized object. Even if you modify the object that was serialized, the serialized object will not be altered and when you deserialize it, the original state only would be returned.

    Anand.

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers