//Copyright (c) 1999, Art Gittleman //This example is provided WITHOUT ANY WARRANTY either expressed or implied. /* Illustrates object persistence */ import java.io.*; public class ObjectIO { public static void main(String [] args) { try { Person fred = new Person("Fred"); Account general = new Account(fred, 110.0); Account savings = new SavingsAccount(fred, 500.0, 6.0); ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("Objects.dat")); oos.writeObject(general); oos.writeObject(savings); oos.close(); ObjectInputStream ois = new ObjectInputStream( new FileInputStream("Objects.dat")); Account aGeneral = (Account)ois.readObject(); Account aSavings = (Account)ois.readObject(); if (aGeneral instanceof SavingsAccount) System.out.println("aGeneral account is a SavingsAccount"); else if (aGeneral instanceof Account) System.out.println("aGeneral account is an Account"); if (aSavings instanceof SavingsAccount) System.out.println("aSavings account is a SavingsAccount"); else if (aSavings instanceof Account) System.out.println("aSavings account is an Account"); if (aGeneral.holder == aSavings.holder) System.out.println("The account holder, fred, is shared"); else System.out.println("The account holder, fred, has been duplicated"); ois.close(); }catch (IOException ioe) { ioe.printStackTrace(); }catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); } } } class Person implements Serializable{ String name; Person (String name) { this.name=name; } } class Account implements Serializable { Person holder; double balance; Account(Person p, double amount) { holder=p; balance=amount; } } class SavingsAccount extends Account implements Serializable { double rate; SavingsAccount(Person p, double amount, double r) { super(p,amount); rate=r; } }