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 / Book.java next >
Encoding:
Java Source  |  1999-09-19  |  3.4 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.  
  22. import java.io.*;
  23.  
  24.  
  25. /**
  26.  * The Externalizable subclass.
  27.  * Will save the state of its superclass by calling the superclass's 
  28.  * writeExternal and readExternal Methods from within its own writeExternal 
  29.  * and readExternal methods
  30.  */
  31. public class Book extends ReadingMaterial implements Externalizable {
  32.  
  33.     private int numpages;
  34.     private String name;
  35.     private boolean ishardcover;
  36.  
  37.     // other relevant information and methods
  38.     // .
  39.     // .
  40.     // .
  41.    
  42.     /** 
  43.      * mandatory public no-arg constructor 
  44.      */
  45.     public Book() { 
  46.     super(); }
  47.   
  48.     public Book(int pages, String n, boolean hardcover, String author,
  49.      String subject, int yearwritten) {
  50.  
  51.     super(author, subject, yearwritten);
  52.     numpages = pages;
  53.     name = n;
  54.     ishardcover = hardcover;
  55.     }
  56.  
  57.     /**
  58.      * Mandatory writeExternal method. 
  59.      * 
  60.      * @serialData Saves state of its superclass by
  61.      *             calling its writeExternal method and 
  62.      *             then save its own fields. Writes numpages field as int,
  63.      *             name field as an object and ishardcover field as a boolean.
  64.      * 
  65.      * @see ReadingMaterial#writeExternal(ObjectOutput)
  66.      */
  67.     public void writeExternal(ObjectOutput out) throws IOException {
  68.     
  69.     // first we call the writeExternal of the superclass as to write
  70.     // all the superclass data fields
  71.     super.writeExternal(out);
  72.  
  73.     // now we take care of this class's fields
  74.     out.writeInt(numpages);
  75.     out.writeObject(name);
  76.     out.writeBoolean(ishardcover);
  77.     }
  78.  
  79.     /**
  80.      * Mandatory readExternal method. Will read in the data that we wrote out
  81.      * in the writeExternal method. Restores the state of the superclass 
  82.      * first by calling the superclass's readExternal method. Then, restores 
  83.      * its own fields. These fields MUST BE IN THE SAME ORDER AND TYPE as we
  84.      * wrote them out.
  85.      * By the time, readExternal is called, an object of this class has already
  86.      * been created using the public no-arg constructor, so this method is
  87.      * used to restore the data to all of the fields of the newly created
  88.      * object.
  89.      */
  90.     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  91.  
  92.       // first call the superclass external method
  93.     super.readExternal(in);
  94.     
  95.     // now take care of this subclass's fields
  96.     numpages = in.readInt();
  97.     name = (String)in.readObject();
  98.     ishardcover= in.readBoolean();
  99.   }
  100.  
  101.     /** 
  102.      * Prints out the fields. used for testing!
  103.      */
  104.     public String toString() {
  105.         return("Name: " + name + "\n" + "Author: " + super.getAuthor() + "\n" + "Pages: "
  106. + numpages + "\n" + "Subject: " + super.getSubject() + "\n" + "Year: " + super.getYearwritten() + "\n" );
  107.     }
  108. }
  109.