home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / externsuper / ReadingMaterial.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  3.4 KB  |  108 lines

  1. /*
  2.  * @(#)ReadingMaterial.java    1.1 98/10/03        
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * 
  20.  */
  21.  
  22. import java.io.*;
  23.  
  24. /** 
  25.  * The Externalizable Superclass:
  26.  *  When the Externalizable Subclass Book implements its writeExternal and
  27.  *  readExternal Methods, it needs to save the state of its superclass using 
  28.  * the superclass's writeExternal and readExternal Methods
  29.  */
  30. public class ReadingMaterial implements Externalizable {
  31.  
  32.     /* 
  33.      * In order for us to be able to serialize this data, these must be
  34.      * either serializable/externalizable objects or primitive data types. 
  35.      */
  36.     private  String author;
  37.     private  String subject;
  38.     private  int yearwritten;
  39.  
  40.     // other relevant data and methods 
  41.     // .
  42.     // .
  43.     // .
  44.   
  45.     /* 
  46.      * Must have a public no-arg constructor when implementing Externalizable
  47.      */
  48.     public ReadingMaterial() {}
  49.     
  50.     /** 
  51.      * Initialize the fields
  52.      *
  53.      */
  54.     public ReadingMaterial(String auth, String sub, int year) {
  55.     author = auth;
  56.     subject = sub;
  57.     yearwritten = year;
  58.     }
  59.     
  60.     /**
  61.      * A public field access method, since the data fields are private and 
  62.      * will need to be accessed by the subclass to print them or use them 
  63.      * otherwise.
  64.      */
  65.     public String getAuthor() { 
  66.     return author; }
  67.     /**
  68.      * A field access method, since the data fields are private and will need
  69.      * to be accessed by the subclass to print them or use them otherwise.
  70.      */
  71.     public String getSubject() { 
  72.     return subject; }
  73.     /**
  74.      * A field access method, since the data fields are private and will need
  75.      * to be accessed by the subclass to print them or use them otherwise.
  76.      */
  77.     public int getYearwritten() { 
  78.     return yearwritten; }
  79.  
  80.     /**
  81.      * Mandatory writeExternal method. 
  82.      * @serialData Write author and subject field as objects and then write
  83.      *             yearwritten field as an integer.
  84.      */
  85.     public void writeExternal(ObjectOutput out) throws IOException {
  86.     
  87.     out.writeObject(author);
  88.     out.writeObject(subject);
  89.     out.writeInt(yearwritten);
  90.   }
  91.  
  92.     /**
  93.      * Mandatory readExternal method. Will read in the data that we wrote out
  94.      * in the writeExternal method. MUST BE IN THE SAME ORDER and type as we
  95.      * wrote it out. By the time, readExternal is called, an object of this 
  96.      * class has already been created using the public no-arg constructor,
  97.      * so this method is used to restore the data to all of the fields of the 
  98.      * newly created object.
  99.      */
  100.   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  101.    
  102.     author = (String)in.readObject();
  103.     subject = (String)in.readObject();
  104.     yearwritten = in.readInt();
  105.   }
  106. }
  107.  
  108.