// This example is from the book Developing Java Beans by Robert Englander.
// Copyright (c) 1997 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.
// Chapter 5 -- The Example9 class and associated classes
import java.io.*;
class AA implements java.io.Externalizable
{
protected int a = 0;
public void writeExternal(ObjectOutput stream)
throws java.io.IOException
{
System.out.println("writeExternal called for class AA");
stream.writeInt(a);
}
public void readExternal(ObjectInput stream)
throws java.io.IOException
{
System.out.println("readExternal called for class AA");
a = stream.readInt();
}
public AA()
{
}
}
class BB extends AA
{
protected int b = 0;
public void writeExternal(ObjectOutput stream)
throws java.io.IOException
{
super.writeExternal(stream);
System.out.println("writeExternal called for class BB");
stream.writeInt(b);
}
public void readExternal(ObjectInput stream)
throws java.io.IOException
{
super.readExternal(stream);
System.out.println("readExternal called for class BB");
b = stream.readInt();
}
public BB()
{
super();
}
}
class CC extends BB
{
protected int c = 0;
public void writeExternal(ObjectOutput stream)
throws java.io.IOException
{
super.writeExternal(stream);
System.out.println("writeExternal called for class CC");
stream.writeInt(c);
}
public void readExternal(ObjectInput stream)
throws java.io.IOException
{
super.readExternal(stream);
System.out.println("readExternal called for class CC");
c = stream.readInt();
}
public CC()
{
super();
}
}
public class Example9
{
// the application entry point
public static void main(String[] args)
{
// create an instance of class CC
CC c = new CC();
try
{
FileOutputStream f = new FileOutputStream("Example9.tmp");
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(c);
s.flush();
}
catch (Exception e)
{
System.out.println(e);
}
try
{
FileInputStream f = new FileInputStream("Example9.tmp");
ObjectInputStream s = new ObjectInputStream(f);
c = (CC)s.readObject();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
|