Sunday, October 31, 2010

Object Serialization

Question : What is Object Serialization ?
Ans :      Object serialization is the process of saving an object's state 
 to a sequence of bytes, as well as the process of rebuilding those bytes 
 into a live object at some future time.


How to make an Object serializable ?
Ans : By implementing the "Serializable Interface".
        An object is marked serializable by implementing the java.io.Serializable
       interface, which signifies to the underlying API that the object can be flattened 
       into bytes and subsequently inflated in the future.


What is Serialization ?
Ans :      Serialization is the process of writing complete state of java object into 
output stream, that stream can be file or byte array or stream associated with 
TCP/IP socket.


What is Marker Interface ?
An:  A Marker Interface is a Interface which may Contain a Method.But a Marker
 Interface should tell a special Information to JVM. that what type of class or object
 it can be.


Example :
 EX. 1) Cloneable is a marker interface with no method which means but
it provide information to the JVM that class's object who implement it
can be cloned.

2) Runnable is also a marker interface with a run() method which provide 
the information to the JVM that class's object which implement this is used
 as a thread.
 
3) while the collection interface doesn't provide any special information to the
 JVM that's why it is not as a Marker interface.

Example for Object Serialization ?
Ans : 
   class Name : PersistentTime.java whose object is serializable.
-----------------------------------------------------------------------------
package newPackage;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

public class PersistentTime implements Serializable{

 private Date time;
 
 public PersistentTime(){
  
  time = Calendar.getInstance().getTime();
  System.out.println(" Time : "+time);
 }
 public Date getTime(){
  
  return time;
 }
 
}
-------------------------------------------------------------------------------------------
Class Name : SerializeObject.java  using this Serializable the persistentTIme Object.



package newPackage;


import java.io.FileOutputStream;
import java.io.ObjectOutputStream;


public class SerializeObject {

public static void main(String[] args){
String fileName = "SerialObject.txt";
FileOutputStream fos = null;
ObjectOutputStream oos = null;
PersistentTime pt = new PersistentTime();
try{
fos = new FileOutputStream(fileName);
oos = new ObjectOutputStream(fos);
oos.writeObject(pt);
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}


-----------------------------------------------------------------------------------------------------------


Class Name : DeserializeObject.java used to de-serialize the Object.

package newPackage;


import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Calendar;


public class DeserializeObject {

public static void main(String[] args) {
String fileName = "SerialObject.txt";
PersistentTime pt = null;
FileInputStream fis = null;
ObjectInputStream ois = null;

try
{
fis = new FileInputStream(fileName);
ois = new ObjectInputStream(fis);
pt = (PersistentTime)ois.readObject();
ois.close();

}catch(Exception e){
e.printStackTrace();
}
System.out.println(" Seraible time "+pt.getTime());
System.out.println(" Current Time : "+Calendar.getInstance().getTime());
}
}
----------------------------------------------------------------------------------------------------------


Excellent Question : What if a Class having a Super class, and the super class 
implements the Serializable interface, but we don't want the subclass to be serializable.


---------------------------  STOP Serialization ----------------------------------------------


Answer : by creating private Methods , and throw the Exception while serializing an Object.
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.


What is Transient Keyword?
Ans: The serialization mechanism simply skips over the transient variables. 


What is serialPersistentFields?
Ans : Declaring serialPersistentFields is almost the opposite of declaring some fields
 transient. The meaning of transient = "This field shouldn't be stored by serialization,"
and  serialPersistentFields =  "These fields should be stored by serialization.











No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...