Before understanding Externalizable interface, you need to have idea about Serialization.You can read more about Serialization at Serialization in java.
Java provides mechanism called serialization to persists java objects in a form of ordered or sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.

Externalizable:

As name suggest it is externalilizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshalling and unmarshalling of objects.Externalizable interface extends Serializable interface. If you implement this interface then you need to override following methods.

1

2

3

4

5

6

7

8

9

10

11

12

13

@Override

publicvoidreadExternal(ObjectInput arg0)throws IOException,

ClassNotFoundException{

}

@Override

publicvoidwriteExternal(ObjectOutput arg0)throwsIOException{

}

Now lets see how serialization happens:

At sender side:
JVM checks if class implements externalizable or not.If it does then serialize object using writeExternal() method.If it does not implement externalizable but implements serializable , object is serialized using ObjectOutputStream.

At receiver side:
When object is reconstructed and it is externalizable , an instance is created using no args constructor and readExternal is called.If it is not externalizable but serializable , object is reconstructed using ObjectInputStream.

Lets start with example same as we have used in Serialization in java.
Create Employee.java in src->org.arpit.java2blog

Employee.java:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

packageorg.arpit.java2blog;

import java.io.Externalizable;

import java.io.IOException;

import java.io.ObjectInput;

import java.io.ObjectOutput;

publicclassEmployeeimplementsExternalizable{

intemployeeId;

StringemployeeName;

Stringdepartment;

Stringnationality;

publicEmployee()

{

}

publicintgetEmployeeId(){

returnemployeeId;

}

publicvoidsetEmployeeId(intemployeeId){

this.employeeId=employeeId;

}

publicStringgetEmployeeName(){

returnemployeeName;

}

publicvoidsetEmployeeName(StringemployeeName){

this.employeeName=employeeName;

}

publicStringgetDepartment(){

returndepartment;

}

publicvoidsetDepartment(Stringdepartment){

this.department=department;

}

publicStringgetNationality(){

returnnationality;

}

publicvoidsetNationality(Stringnationality){

this.nationality=nationality;

}

@Override

publicvoidreadExternal(ObjectInput in)throws IOException,

ClassNotFoundException{

employeeId=in.readInt();

employeeName=(String)in.readObject();

}

@Override

publicvoidwriteExternal(ObjectOutput out)throwsIOException{

out.writeInt(employeeId);

out.writeObject(employeeName);

}

}

you must have no args contructor if you implement externalizable.

Create ExternalizableMain.java in org.arpit.java2blog

ExternalizableMain.java:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

packageorg.arpit.java2blog;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

publicclassExternalizableMain{

/**

* @author Arpit Mandliya

*/

publicstaticvoidmain(String[]args){

Employee emp=newEmployee();

emp.setEmployeeId(101);

emp.setEmployeeName("Arpit");

emp.setDepartment("CS");

//Serialize

try

{

FileOutputStream fileOut=newFileOutputStream("employee.ser");

ObjectOutputStream outStream=newObjectOutputStream(fileOut);

outStream.writeObject(emp);

outStream.close();

fileOut.close();

}catch(IOExceptioni)

{

i.printStackTrace();

}

//Deserialize

emp=null;

try

{

FileInputStream fileIn=newFileInputStream("employee.ser");

ObjectInputStream in=newObjectInputStream(fileIn);

emp=(Employee)in.readObject();

in.close();

fileIn.close();

}catch(IOExceptioni)

{

i.printStackTrace();

return;

}catch(ClassNotFoundExceptionc)

{

System.out.println("Employee class not found");

c.printStackTrace();

return;

}

System.out.println("Deserialized Employee...");

System.out.println("Emp id: "+emp.getEmployeeId());

System.out.println("Name: "+emp.getEmployeeName());

}

}

Run it :

When you run ExternalizableMain.java.You will get following output

1

2

3

4

5

Deserialized Employee...

Emp id:101

Name:Arpit

If you already have serializable,why you need externalizable at all!!:

When you serialize any object using serializable, apart from fields, all objects that belong to object map and that can be reached using instance variable will also be serialized .for example :

If you have Employee class and its superclass is person then it will serialize all superclass objects (such as person) until it reaches “Object” class.

Similarly if Employee has instance variable of address class then it will serialize whole object map of address also .

Do you really want this much overhead when all you want to serialize is employeeId and employeeName

JVM uses reflection when you use serializable which is quite slow.

While serializing,information about class description which incluses description of its superclass and instance variable associated with that class also get stored in stream.Again this is also a performance issue

Inheritance in Externalization:

Now we will see how inheritance affects externalization.So there can be muliple cases whether super class is externalizable or not.If not then how will you handle that and how it works.Lets see by example.
We will create Person.java which will be superclass of Employee.

Case 1: What if super class does not implement Externalizable:

If superclass does not implements externalizable , you need to serialize superclass ‘s fields in subclass that implements Externalizable.

In this example, since the Person class stores and restores its fields in its own writeExternal and readExternal methods, you dont need to save/restore the superclass fields in sub class but if you observe closely the writeExternal and readExternal methods of Employee class, you will find that you still need to first call the super.xxxx() methods that confirms the statement the externalizable object must also coordinate with its supertype to save and restore its state.

DownSides of Externalizable:

If you make any change to your class definition, you need to maintain writeExternal() and readExternal accordingly.

As we have seen in example,Sub class object has to coordinate with its superclass to save and store its state(by call super.xxxx() method from subclass)