Java Reference
In-Depth Information
TheimplementationofthePetStoresystemisbasedontheclassdia-
gramin Figure16-4 . TheclassPetholdsvariablesforthename,location,
and price of all pets in the store. It also contains three concrete methods
to display the name, location, and price of each pet object. In order to
preserve encapsulation, the accessor methods for the class-level attrib-
utes must be in the superclass. Recall that, with or without inheritance,
private data is not visible outside the class.
In addition, the superclass Pet contains an abstract method named
getSpecData(). The getSpecData() method is implemented in each of the
subclasses. In the Dog class, getSpecData() displays the dogBreed. In the
Cat class, getSpecData() displays the cat name. In the Bird class,
getSpecData() displays the bird color. The following code listing is for the
PetStore program.
On the Web
The program PetStore is found in the Chapter 16 folder at
www.crcpress.com .
//**********************************************************
//**********************************************************
// Program: PetStore
// Reference: Chapter 16
// Topics:
// 1. Using inheritance and abstract classes
//**********************************************************
//**********************************************************
//******************************************
//******************************************
// CLASS Pet
//******************************************
//******************************************
abstract class Pet
{
//********************************
// attributes section
//********************************
// Instance fields
private String petName;
private int petLocation;
private double petPrice;
//*********************************
// methods section
//*********************************
// Constructor.
// Defines the initial pet state
Search WWH ::




Custom Search