0
Sponsored Links


Ad by Google
Still on demand in first round of technical interview discussion, specially for 3-5 years of experienced. This question most of the time asked in telecom and finance domain interview specially in telecom domain.
OK, In this post we are going to highlight few key differences between Serializable and Externalizable interface which you must need to keep in mind while going for an interview, before that we would request you to please go through our previous tutorial about Serializable and Externalizable interface.

Here we have listed five key differences between java.io.Serializable and java.io.Externalizable interface.
  1. java.io.Serializable is a marker interface means empty interface, whereas java.io.Externalizable has two methods writeExternal() and readExternal().
  2. In Serializable interface, at the time of deserialization(restoring) the object from the serialized stream no constructor will call, whereas In Externalizable interface, at the time of deserialization(restoring) the object from the serialized stream a public no-arg constructor will call (It is mandatory to have public no-arg constructor while implementing Externalizable interface otherwise at the time of restoring the object it will throws java.io.InvalidClassException: no valid constructor).
  3. By implementing java.io.Serializable interface you will get automatic capability of serialization for your object. No need to implement any other logic, whereas by implementing java.io.Externalizable interface you have to maintain the logic of serialization yourself by overriding writeExternal() and readExternal() methods.
  4. For performance point of view java.io.Serializable interface is less important than java.io.Externalizable interface. Although Reflection API's was improved since java 1.3, Still Externalizable is better than Serializable(By implementing Serializable interface java run time uses reflection api to serialized the object, whereas Externalizable provides the custom-written methods to serialized the object using writeExternal() and readExternal() methods to get around the reflection performance bottleneck).
  5. In java.io.Serializable interface you can customized the default protocol by providing these methods:
    1. private void writeObject(ObjectOutputStream out) throws IOException;
    2. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
    Whereas in java.io.Externalizable interface you can create your own protocol by overriding these methods:
    1. public void writeExternal(ObjectOutput out) throws IOException;
    2. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;

Here is a sample program to serialized an object using java.io.Serializable interface.
AI.java class

package com.javamakeuse.poc;

import java.io.Serializable;

public class AI implements Serializable {
 private String secret;

 public AI(String secret) {
  super();
  this.secret = secret;
 }

 public String getSecret() {
  return secret;
 }

 @Override
 public String toString() {
  return "AI [secret=" + secret + "]";
 }

}
AIMain.java class to serialized/deserialized the AI.java

package com.javamakeuse.poc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class AIMain {

 private static final String file = "ai.ser";

 private static void serialized(Object obj) throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  ObjectOutputStream out = new ObjectOutputStream(fos);
  out.writeObject(obj);
  out.close();
 }

 private static AI deserialized() throws IOException, ClassNotFoundException {
  FileInputStream fis = new FileInputStream(file);
  ObjectInputStream in = new ObjectInputStream(fis);
  return (AI) in.readObject();
 }

 public static void main(String[] args) {
  try {
   // writing into stream.
   serialized(new AI("secret"));

   // restoring object
   AI ai = deserialized();
   System.out.println("restoring value="+ai.getSecret());

  } catch (IOException | ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
Run the AIMain class, it will serialized the object and also restore the object from the stream and print at you console.
OUTPUT: restoring value= secret

OK, now lets see the simple implementation of java.io.Externalizable interface.

AI.java class

package com.javamakeuse.poc;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class AI implements Externalizable {
 private String secret;

 public AI() {
 }

 public AI(String secret) {
  super();
  this.secret = secret;
 }

 public String getSecret() {
  return secret;
 }

 @Override
 public String toString() {
  return "AI [secret=" + secret + "]";
 }

 @Override
 public void writeExternal(ObjectOutput out) throws IOException {
  out.writeObject(secret);
 }

 @Override
 public void readExternal(ObjectInput in) throws IOException,
   ClassNotFoundException {
  secret = (String) in.readObject();
 }

}
To test the implementation of Externalizable interface Run the AIMain.java class. No changes is required in serialized and deserialized method of AIMain.java class it work just fine.


That's it, for more in detail on Serializable and Externalizable, you can visit on these tutorials What is Serializable? and Why Externalizable?


References:
Reference 1
Reference 2

Sponsored Links

0 comments:

Post a Comment