home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / nonserialsuper / Book.java next >
Encoding:
Java Source  |  1999-09-19  |  3.5 KB  |  109 lines

  1. /*
  2.  * @(#)Book.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. import java.io.*;
  22.  
  23. class Book extends ReadingMaterial implements Serializable {
  24.  
  25.     int numpages;
  26.     String name;
  27.     boolean ishardcover;
  28.  
  29.     // other relevant information and methods
  30.     // .
  31.     // .
  32.     // .
  33.  
  34.     Book(int pages, String n, boolean hardcover, String author,
  35.      String subject, int yearwritten) 
  36.     {
  37.     super(author, subject, yearwritten);
  38.     numpages = pages;
  39.     name = n;
  40.     ishardcover = hardcover;
  41.     }
  42.  
  43.     /**
  44.      * Saves its own fields by calling defaultWriteObject and then explicitly
  45.      * saves the fields of its supertype  
  46.      * 
  47.      * @serialData Store own serializable fields by calling defaultWriteObject
  48.      *             and save supertype fields as optional data. Optional
  49.      *             data is written in following sequence; author field 
  50.      *             is written as object, subject is an object and the
  51.      *             yearwritten field is written as an integer.
  52.      */
  53.     private void writeObject(ObjectOutputStream out)  throws IOException {
  54.         
  55.     // Take care of this class's field first by calling defaultWriteObject
  56.     out.defaultWriteObject();
  57.     
  58.     /*
  59.      * Since the superclass does not implement the Serializable interface
  60.      * we explicitly do the saving... Since these fields are not private
  61.      * we can access them directly. If they were private, the superclass
  62.      * would have to implement get and set methods that would allow the
  63.      * subclass this necessary access for proper saving.
  64.      */
  65.     out.writeObject(author);
  66.     out.writeObject(subject);
  67.     out.writeInt(yearwritten);
  68.     }
  69.  
  70.     /**
  71.      * Restores its own fields by calling defaultReadObject and then explicitly
  72.      * restores the fields of its supertype. 
  73.      */
  74.     private void readObject(ObjectInputStream in) 
  75.     throws IOException, ClassNotFoundException {
  76.     
  77.         /*
  78.          * Take care of this class's fields first by calling 
  79.          * defaultReadObject
  80.          */
  81.         in.defaultReadObject();
  82.     
  83.         /* 
  84.          * Since the superclass does not implement the Serializable 
  85.          * interface we explicitly do the restoring... Since these fields 
  86.          * are not private we can access them directly. If they were 
  87.          * private, the superclass would have to implement get and set 
  88.          * methods that would allow the subclass this necessary access 
  89.          * for proper saving or restoring.
  90.          */
  91.         author = (String) in.readObject();
  92.         subject = (String) in.readObject();
  93.         yearwritten = in.readInt();
  94.     }
  95.  
  96.     /** Print out the field values. Useful for testing.
  97.      */
  98.    public String toString() {
  99.     return("Name: " + name + "\n" + "Author: " + author + "\n" + "Pages: "
  100.            + numpages + "\n" + "Subject: " + subject + "\n" + "Year: " + yearwritten
  101.            + "\n");
  102.     }
  103. }
  104.  
  105.  
  106.   
  107.  
  108.  
  109.