// File: TestPoly2.java // // Author: Rahul Simha // Created: Sept 16, 1998 // Modified: Sept, 2008 // // Illustrates casting among objects. class Customer extends Person { // New data. int loyalty; // Number of years as customer. // New constructor. public Customer (String nameIn, String ssnIn, int loyaltyIn) { // This is like calling Person (nameIn, ssnIn). super (nameIn, ssnIn); // Subclass data. loyalty = loyaltyIn; } // Override toString() public String toString () { return "Customer: name=" + name + ", " + loyalty + " years with the company"; } // Accessor: public int getLoyalty () { return loyalty; } } // End of class "Customer" // Testing. public class TestPoly2 { public static void main (String[] argv) { Customer c = new Customer ("Gullible Gus", "555-55-5678", 5); // Automatic cast. Person p = c; // Explicit cast required. Customer c2 = (Customer) p; } } // End of class "TestPoly2"