Page 525 - Introduction to Programming with Java: A Problem Solving Approach
P. 525

                12.9 Using Inheritance with Aggregation and Composition 491
What sort of inheritance relationship could/should be added to the Dealership program? If you look back at Figures 12.5 (Manager class) and 12.6 (SalesPerson class), you’ll see that Manager and SalesPerson both declare the same instance variable, name, and they both define the same instance method, getName. That’s an example of undesirable duplication, and we can use inheri-
tance to eliminate that duplication. Introducing inheritance into that program does not al-
ter the original whole-parts structure. It just introduces a complementary mechanism that
eliminates duplication.
Figure 12.14 shows an improved and expanded UML class diagram for a new Dealership2 program. If you compare this with the previous UML class diagram, you’ll see that each class is fleshed out to include
  Factor out the common code.
   Dealership2
 -company : String
-manager : Manager2
-people : ArrayList<SalesPerson2> -cars : ArrayList<Car>
 +Dealership2(company : String, manager : Manager2) +addPerson(person : SalesPerson2) : void +addCar(car : Car) : void
+printStatus() : void
    top of composition hierarchy
  ⎫⎪1 1 1⎫⎪
     Apago PDF Enhancer
 aggregation
⎬⎪ ⎬⎪ ⎭1*⎭
*
  relationships
 Manager2
  +Manager2(name : String)
 Car
 -make : String
 +Car(make : String) +getMake() : String
      SalesPerson2
 -sales : double = 0.0
 +SalesPerson2(name : String)
   ⎫ ⎬
⎭
inheritance relationships
composition relationship
    Person
 -name : String
 +Person() +Person(name : String) +getName() : String
     top of inheritance hierarchy
 Figure 12.14 Class diagram for revised car dealership program—Dealership2






























































   523   524   525   526   527