Java Reference
In-Depth Information
The field variables are now defined in the client classes Dog, Cat, and Bird.
This does not mean that more data is stored by the code, since each object
has its own state in either case.
The client classes Dog, Cat, and Bird contain an object of the host class Pet.
The object is named aPet in all the client classes.
The code must also be changed to reflect the new model. The program
PetStore2, listed below, shows the new version. To save space we have
implemented only the classes Pet and Dog in the PetStore2 program.
On the Web
The source file for the program PetStore2.java can be found in the
Chapter17folderat www.crcpress.com .
//**********************************************************
//**********************************************************
// Program: PetStore2
// Reference: Chapter 17
// Topics:
// 1. Class reuse through object composition
//**********************************************************
//**********************************************************
//
//******************************************
//******************************************
// CLASS Pet
//******************************************
//******************************************
class Pet
{
// Concrete methods in the base class
public void getName(String aName)
{
System.out.println ("Pet name: " + aName);
return;
}
public void getLocation(int aLocation)
{
System.out.println("Pet location: " + aLocation);
return;
}
public void getPrice(double aPrice)
{
System.out.println("Pet price: " + aPrice);
return;
}
}
//************************************
Search WWH ::




Custom Search