Developing Java Beans private void readObject(ObjectInputStream stream) throws
Developing Java Beans } } Now we can run this application, and during the writing and reading of the instance of class C, we will get output from the writeObject() and readObject() methods of each class in the hierarchy. Run the application with the following command: java Example7 When the application is run, an instance of class C is created, written to a stream, then read back. In doing so, the following output is generated to the console window: writeObject called for class AwriteObject called for class BwriteObject called for class CreadObject called for class AreadObject called for class BreadObject called for class C As you can see from the output, the serialization process walks up the class hierarchy to the highest- level superclass that implements java.io.Serializable, and then walks back down to serialize data from each class. The sequencing of output from class A to class B to class C shows this clearly. Let’s create a more complex example to illustrate the use of class-specific serialization. First, we define a class called ListeningPanel. This class extends java.awt.Panel, which itself extends java.awt.Container, and it contains three instances of java.awt.Button named b1, b2, and b3. The ListeningPanel uses a GenericButtonAdapter to listen for action events from the buttons and routes them to the methods called handleB1(), handleB2(), and handleB3(), respectively. This example poses a problem with the use of the GenericButtonAdapter class because it isn’t serializable. Furthermore, we can’t trivially make the adapter serializable by adding implements Serializable to the class definition, because it contains a hash table that holds Method objects, and the Method class can’t be serialized. We could mark the instance of GenericButtonAdapter within the ListeningPanel class as transient. Doing so would exclude the adapter from the process of serializing an instance of ListeningPanel. But the ListeningPanel class is not the only one that will have a reference to the GenericButtonAdapter instance. The adapter is the registered listener of action events for the buttons. These buttons are added to the panel using the add() method, and references to them are kept by the base java.awt.Container class. These relationships are shown in Figure 5.4. Figure 5.4. Object relationships for ListeningPanel page 82
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services