Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8.2 Aggregation and Inheritance. © 2006 Pearson Addison-Wesley. All rights reserved 8.2.2 Designing with Aggregation and Inheritance Software.

Similar presentations


Presentation on theme: "Lecture 8.2 Aggregation and Inheritance. © 2006 Pearson Addison-Wesley. All rights reserved 8.2.2 Designing with Aggregation and Inheritance Software."— Presentation transcript:

1 Lecture 8.2 Aggregation and Inheritance

2 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.2 Designing with Aggregation and Inheritance Software Design Step 1 Identify the objects. Step 2 Find/design a class for each object. One of the Challenges deciding when to use aggregation and when to use inheritance SupplierClass ClientClass SuperClass SubClass

3 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.3 What is AGGREGATION? public class SomeAggregate { private ClassA partA; private ClassB partB; private ClassC partC1; private ClassC partC2; // methods omitted } public class SomeAggregate { private ClassA partA; private ClassB partB; private ClassC partC1; private ClassC partC2; // methods omitted } SomeAggregate ClassAClassBClassC

4 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.4 Class Diagram public class SomeAggregate { private ClassA partA; private ClassB partB; private ClassC partC1; private ClassC partC2; // methods omitted } public class SomeAggregate { private ClassA partA; private ClassB partB; private ClassC partC1; private ClassC partC2; // methods omitted } SomeAggregate ClassAClassBClassC Object Diagram :SomeAggregate partA : ClassA partB : ClassBpartC1 : ClassC partC2 : ClassC

5 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.5 When to aggregate? One class contains_a other. A dog has_a tail. An equilateral triangle contains_a vertex. A sandal includes_a sole. A company’s work roster contains_an employee. Cherry pie contains_a cherry pit. contains_a

6 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.6 When to inherit? One class is_a another class A dog is_an animal. An equilateral triangle is_a triangle. A sandal is_a special kind of footware. An hourly employee is_an employee. Cherry pie is_a dessert. is_a Generalizations can be discovered from common attributes.

7 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.7 Create the best relationship from... camcorder record button tape recorder lens on/off switch viewfinder tape recordercamcorderrecord buttonon/off switchlensviewfinder

8 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.8 A Dilemma (which is it?) A Camcorder is_a tape recorder. or A Camcorder contains_a tape recorder. Either aggregation or inheritance will work in this case (see next slide).

9 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.9 public class Camcorder { private TapeRecorder rec; //somewhere in the code rec.startRecording(); } public class Camcorder { private TapeRecorder rec; //somewhere in the code rec.startRecording(); } public class Camcorder extends TapeRecorder { //somewhere in the code startRecording(); } public class Camcorder extends TapeRecorder { //somewhere in the code startRecording(); }

10 © 2006 Pearson Addison-Wesley. All rights reserved 8.2.10 java.awt.Container Container is an awt class well suited to inheritance. An Container subclass can reuse...  add and remove  setBounds and setLocation  getX, getY, getWidth, getHeight, getBackground  repaint A Container subclass may want to override...  setSize and setBounds  setBackground A Container object is a transparent rectangle. It is useful for grouping other graphical objects into a single unit.


Download ppt "Lecture 8.2 Aggregation and Inheritance. © 2006 Pearson Addison-Wesley. All rights reserved 8.2.2 Designing with Aggregation and Inheritance Software."

Similar presentations


Ads by Google