home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / nonexternsuper / Book.java next >
Encoding:
Java Source  |  1999-09-19  |  3.7 KB  |  120 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.  
  22. import java.io.*;
  23.  
  24. /** 
  25.  * The subclass that implements Externalizable.. responsible for
  26.  * saving the state of its non-Externalizable super class
  27.  */
  28. class Book extends ReadingMaterial implements Externalizable {
  29.  
  30.     int numpages;
  31.     String name;
  32.     boolean ishardcover;
  33.  
  34.     // other relevant information and methods
  35.     // .
  36.     // .
  37.     // .
  38.    
  39.     /*
  40.      * mandatory public no-arg constructor
  41.      * if the superclass did not have a no-arg constructor, we would have to
  42.      * give the regular constructor default values.
  43.      */
  44.     public Book() { super(); }
  45.     
  46.     Book(int pages, String n, boolean hardcover, String author,
  47.      String subject, int yearwritten) {
  48.  
  49.     super(author, subject, yearwritten);
  50.     numpages = pages;
  51.     name = n;
  52.     ishardcover = hardcover;
  53.     }
  54.  
  55.     /** 
  56.      * Mandatory writeExernal method. 
  57.      *
  58.      * @serialData Expicitly saves the supertype's fields and then saves this
  59.      *             class fields. Writes superclass fields in following order:
  60.      *             author as an object, subject as an object, yearwritten
  61.      *             as an integer. Writes class fields in following order:
  62.      *             numpages as an int, name as an Object and ishardcover 
  63.      *             as a boolean.
  64.      */
  65.     public void writeExternal(ObjectOutput out) throws IOException  {
  66.  
  67.     /* 
  68.      * Since the superclass does not implement the Serializable interface
  69.      * we explicitly do the saving... Since these fields are not private
  70.      * we can access them directly. If they were private, the superclass
  71.      * would have to implement get methods that would allow the
  72.      * subclass this necessary access for proper saving.
  73.      */
  74.     out.writeObject(author);
  75.     out.writeObject(subject);
  76.     out.writeInt(yearwritten);
  77.        
  78.     // now we take care of this subclass's fields
  79.     out.writeInt(numpages);
  80.     out.writeObject(name);
  81.     out.writeBoolean(ishardcover);
  82.     }
  83.  
  84.     /** 
  85.      * Mandatory readExternal method. Explicitly restores the supertype's 
  86.      * fields and then restores
  87.      *  this class's fields
  88.      */
  89.     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  90.  
  91.     /* 
  92.      * Since the superclass does not implement the Serializable interface
  93.      * we explicitly do the restoring... Since these fields are not private
  94.      * we can access them directly. If they were private, the superclass
  95.      * would have to implement set methods that would allow the
  96.      * subclass this necessary access for proper restoring.
  97.      */
  98.     author = (String) in.readObject();
  99.     subject = (String) in.readObject();
  100.     yearwritten = in.readInt();
  101.  
  102.     // now we take care of the subclass's fields
  103.     numpages = in.readInt();
  104.     name = (String) in.readObject();
  105.     ishardcover = in.readBoolean();
  106.     }
  107.  
  108.     /** 
  109.      * Prints out the fields. used for testing!
  110.      */
  111.     public String toString() {
  112.         return("Name: " + name + "\n" + "Author: " + author + "\n" + "Pages: "
  113. + numpages + "\n" + "Subject: " + subject + "\n" + "Year: " + yearwritten + "\n");
  114.     }
  115. }
  116.  
  117.   
  118.  
  119.  
  120.