Source of PolymorphismTest.java


  1: // Fig. 10.1: PolymorphismTest.java
  2: // Assigning superclass and subclass references to superclass and
  3: // subclass variables.
  4: 
  5: public class PolymorphismTest  
  6: {
  7:    public static void main( String args[] ) 
  8:    {
  9:       // assign superclass reference to superclass variable
 10:       CommissionEmployee3 commissionEmployee = new CommissionEmployee3( 
 11:          "Sue", "Jones", "222-22-2222", 10000, .06 );                    
 12: 
 13:       // assign subclass reference to subclass variable
 14:       BasePlusCommissionEmployee4 basePlusCommissionEmployee = 
 15:          new BasePlusCommissionEmployee4( 
 16:          "Bob", "Lewis", "333-33-3333", 5000, .04, 300 );         
 17: 
 18:       // invoke toString on superclass object using superclass variable
 19:       System.out.printf( "%s %s:\n\n%s\n\n", 
 20:          "Call CommissionEmployee3's toString with superclass reference ",
 21:          "to superclass object", commissionEmployee.toString() );
 22: 
 23:       // invoke toString on subclass object using subclass variable      
 24:       System.out.printf( "%s %s:\n\n%s\n\n", 
 25:          "Call BasePlusCommissionEmployee4's toString with subclass",
 26:          "reference to subclass object", 
 27:          basePlusCommissionEmployee.toString() );
 28: 
 29:       // invoke toString on subclass object using superclass variable
 30:       CommissionEmployee3 commissionEmployee2 = 
 31:          basePlusCommissionEmployee; 
 32:       System.out.printf( "%s %s:\n\n%s\n", 
 33:          "Call BasePlusCommissionEmployee4's toString with superclass",
 34:          "reference to subclass object", commissionEmployee2.toString() );
 35:    } // end main
 36: } // end class PolymorphismTest
 37: 
 38: /**************************************************************************
 39:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 40:  * Pearson Education, Inc. All Rights Reserved.                           *
 41:  *                                                                        *
 42:  * DISCLAIMER: The authors and publisher of this book have used their     *
 43:  * best efforts in preparing the book. These efforts include the          *
 44:  * development, research, and testing of the theories and programs        *
 45:  * to determine their effectiveness. The authors and publisher make       *
 46:  * no warranty of any kind, expressed or implied, with regard to these    *
 47:  * programs or to the documentation contained in these books. The authors *
 48:  * and publisher shall not be liable in any event for incidental or       *
 49:  * consequential damages in connection with, or arising out of, the       *
 50:  * furnishing, performance, or use of these programs.                     *
 51:  *************************************************************************/