Download presentation
Presentation is loading. Please wait.
Published byQuentin McDaniel Modified over 9 years ago
1
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 1 An Introduction to Object-Oriented Analysis Objects and UML in plain English. Chapter 8: Properties of Objects and Classes Based on the book by David William Brown John Wiley & Sons, ISBN 0471371378
2
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 2 Copyright Copyright 8 2002 Flying Kiwi Productions All rights reserved. This slide presentation is based on “ An Introduction to Object- Oriented Analysis; Objects and UML in Plain English,” by David William Brown, Wiley, ISBN 0471371378, “The Book.” where a copy of The Book is purchased Permission is hereby granted to copy, modify or excerpt all or any part of this slide presentation, provided it is solely for use with courses, seminars or other presentations or productions where a copy of The Book is purchased by or for each and every participant or recipient. An instructor guide is available from the publisher for such presentations.
3
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 3 Chapter 8: Properties of Objects and Classes l 8.0. Review from Chapter 5 - Classes. 8.1. Subclasses and Inheritance. 8.2. More About Inheritance. 8.3. Polymorphism and Overriding. 8.4. Abstract Classes. 8.5. Aggregation: Components and Wholes. 8.6. The Difference Between Subclassing and Aggregation.
4
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 4 Review from Chapter 5: Objects and Classes Classes and Classification
5
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 5 8.0. Classes and Classification Class, Webster: Class, a number of people or things grouped together because of certain likenesses or traits.
6
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 6 l Attributes, l Behavior, l Relationships, and l Semantics (meaning). We need to consider: 8.0. Classes and Classification In the real world
7
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 7 Definition: class similar properties common behavior common relationships common semantics A class is a group of objects with: similar properties (attributes), common behavior (operations), common relationships to other objects, and common semantics (i.e., meaning.) 8.0. Classes and Classification In the real world
8
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 8 l Features not shared by the rest of the class n Attributes n Behavior n Associations The key verb is: Classes and Classification. Some instances of a class may be grouped together on the basis of: “ISAKINDA” 8.0. Classes and Classification Subclasses In the real world
9
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 9 The class of Fire Trucks The class of Animals Subclass of Elephants Subclass of Turtles
10
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 10 UML Hierarchy Diagram ( UML notation )PERSON CUSTOMER EMPLOYEE Balance O/Due 30, 60, 90 Credit Rating Date Paid CheckCrRating AgeBalances Name, Address Phone, Sex Date of Birth ChangeAddress EnquireDOB&Sex SIN Marital Status No. of Dependants Date Hired Wage Rate GiveRaise CalcMonthPay CANBEA ISAKINDA This kind of arrowhead indicates that this relationship is one of subclassing
11
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 11 Abstract Classes l Such classes have no instances other than the ones provided for them by their subclasses Direct Instances That is, they have no Direct Instances Abstract Classes For this reason we call them Abstract Classes 8.0. Classes and Classification Additional Data-world Classes
12
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 12 Chapter 8: Properties of Objects and Classes l 8.0. Review from Chapter 5 - Classes. 8.1. Subclasses and Inheritance. 8.1. Subclasses and Inheritance. 8.2. More About Inheritance. 8.3. Polymorphism and Overriding. 8.4. Abstract Classes. 8.5. Aggregation: Components and Wholes. 8.6. The Difference Between Subclassing and Aggregation.
13
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 13 Subclass selected instances Parent class Superclass A Subclass is made up of selected instances from another class, the “ Parent class ” or “ Superclass.” (See Fig 8.1 on page 219) Superclass ALL A Superclass includes ALL the instances of the Subclass, plus possibly more as well. Inheritance in addition Inheritance is when a subclass instance, in addition to the attributes and behavior it has by virtue of being in the subclass, all the attributes and behavior also has all the attributes and behavior that instances of the superclass have (see Fig 8.2). Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance.
14
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 14 l The subclass relationship actually is a relationship in the way we have used that word, l So it requires a verb (we’ll give it one in each direction), IsakindaCanbea And those verbs are: Isakinda and Canbea isakinda A Clerk isakinda Person canbea A Person canbea Clerk (but doesn’t have to be) isakinda A Car isakinda Vehicle canbea A Vehicle canbea Car (but doesn’t have to be) isakinda A G/L Account isakinda Account canbea An Account canbea G/L Account isakinda A Cash Customer isakinda Customer canbea A Customer canbea Cash Customer Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance.
15
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 15 isakinda A Mammal isakinda Animal canbea An Animal canbea Mammal (but doesn’t have to be) isakinda A Freighter isakinda Ship canbea A Ship canbea Freighter (but doesn’t have to) isakinda A Jet isakinda Aircraft canbea An Aircraft canbea Jet (but doesn’t have to be) isakinda A Skater isakinda Athlete canbea An Athlete canbea Skater (but doesn’t have to be) isakinda A Speedskater isakinda Skater canbea A Skater canbea Speedskater (b.d.h.t.b.) Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance.
16
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 16 truly O-O, To be considered truly O-O, a language, database, etc. must support: s Objects s Classes s Inheritance s Polymorphism (to be defined shortly) supporting We can define supporting a feature to mean “easy and convenient” that the language makes it “easy and convenient” for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:
17
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 17 associations Another thing we can do with subclasses is show more detail about associations on our diagram. l For instance, in most companies, only managers can hire and fire. kindsa In other words, only certain kindsa employees can do certain tasks. only a subclass l Thus we are able to show that some associations affect only a subclass, and not every instance. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance.
18
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 18 Union Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Company Vehicle Regular Employee Manager Employee isakinda canbea supervises drives belongs to Next: More About Inheritance... *1 * 1 * 1
19
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 19 Chapter 8: Properties of Objects and Classes l 8.0. Review from Chapter 5 - Classes. 8.1. Subclasses and Inheritance. 8.2. More About Inheritance. 8.2. More About Inheritance. 8.3. Polymorphism and Overriding. 8.4. Abstract Classes. 8.5. Aggregation: Components and Wholes. 8.6. The Difference Between Subclassing and Aggregation.
20
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 20 special case A Payables Account is a special case of an Account. Account (i.e., specialized) Payables What makes this Account different (i.e., specialized) is that it is a Payables Account, Receivables And that other one over there is a Receivables Account. specializing Thus, the Payables and the Receivables subclasses may be viewed as specializing the Account class. specialize only certain of the superclass instances A subclass is said to specialize its superclass(es) by including only certain of the superclass instances. Now let’s take another look at inheritance... Chapter 8: Properties of Objects and Classes 8.2. More About Inheritance. Generalization and Specialization
21
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 21 UML Hierarchy Diagram ( UML notation )PERSON CUSTOMER EMPLOYEE Balance O/Due 30, 60, 90 Credit Rating Date Paid CheckCrRating() AgeBalances() Name, Address Phone, Sex Date of Birth ChangeAddress EnquireDOB&Sex SIN Marital Status No. of Dependants Date Hired Wage Rate GiveRaise() CalcMonthPay() CANBEA ISAKINDA This kind of arrowhead to indicate that this relationship is one of subclassing All these attributes and behaviors are inherited as is by both subclasses Each subclass then adds attributes and behaviors that it needs but the other one doesn’t.
22
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 22 Chapter 8: Properties of Objects and Classes 8.2. More About Inheritance. l Subclassing n Begin from the superclass (parent class) and treat the subclass as special cases (top down) n Begin from the subclasses and find common attributes and behaviors that indicate the need for a superclass l Both approaches are valid and necessary in modeling process. l Fig 8.10 shows the class diagram of multilevel inheritance (page 231)
23
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 23 Chapter 8: Properties of Objects and Classes l 8.0. Review from Chapter 5 - Classes. 8.1. Subclasses and Inheritance. 8.2. More About Inheritance. 8.3. Polymorphism and Overriding. 8.3. Polymorphism and Overriding. 8.4. Abstract Classes. 8.5. Aggregation: Components and Wholes. 8.6. The Difference Between Subclassing and Aggregation.
24
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 24 l Polymorphism s Poly = many s Morph = shape l Polymorphism means “occurring in various forms.” l Along with Inheritance, polymorphism is one of the major keys to the power of objects. (A “primary key”, perhaps?) Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
25
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 25 On the next slide, l You will see a class diagram for handling 9-1-1 emergency calls. l Study it well. “DeleteCall” Take particular note of the “DeleteCall” method that appears in the superclass “911 Call” as well as in both subclasses. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
26
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 26 Call ID Date Time Calling Number Address Create Call Display Details List CallsDeleteCall Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCallDeleteCall Response Specialist 911 Call False Alarm Valid Call Severity CriminalY/NDeleteCall Police Officer Fire Fighter Paramedic handles Vehicle Citizen investigates Weapon Dog * 1 1 * * * ** ** ** CANBEAISAKINDA
27
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 27 l You can see that the amount of work that has to be done deleting a call kinda depends on the kinda call. False Alarm l A False Alarm needs some program code that will disconnect it from the investigating officer as well as the offending citizen. Valid Call l A Valid Call needs program code to disconnect it from: s Many Response Specialists of various kinds, s Many Citizens, and this code it can share with the False Alarm calls, s Many Vehicles, Weapons and Dogs. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
28
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 28 kindsa Now you see that we need at least two different pieces of program code to delete these two kindsa 911 calls. same name!! But I have shown them both with the same name!! l This would never work in regular “procedural” programming, l But it can be made to work here, and this is the power of objects! Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
29
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 29 l Object-Oriented languages allow us to define two functions (i.e., methods) with the same name different classes Provided they are in different classes but they must be subclasses and superclasses. kinda Whichever kinda 911 Call we are handling, the system will choose the right version of the method to suit that particular Call instance. “The object knows its own type, so it knows what to do.” l We say that “The object knows its own type, so it knows what to do.” Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
30
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 30 before it is even evaluated And what about a 911 Call instance that needs to be cancelled before it is even evaluated to see if it was a False Alarm or a Valid Call? l This one is an instance of the superclass, l But not of either subclass. s Let’s say our analysis has discovered that the only difference in processing these calls and the False Alarms is the Investigating Officer, so we have decided to use one piece of program code to do both kindsa 911 Calls. DeleteCall This gives us the diagram on the next slide, with no DeleteCall method in the False Alarm class. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
31
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 31 Call ID Date Time Calling Number Address Create Call Display Details List CallsDeleteCall Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCallDeleteCall Response Specialist 911 Call False Alarm Valid Call Severity CriminalY/N Police Officer Fire Fighter Paramedic handles Vehicle Citizen investigates Weapon Dog * 1 1 * * * ** ** **
32
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 32 l When an Unevaluated 911 Call is to be deleted, only It knows it is an instance only of the superclass, so it looks in there for some code to execute. DeleteCall Of course it finds a DeleteCall method, so it executes it and deletes the call. l When a Valid 911 Call is to be deleted, s It knows it is an instance in the Valid Call subclass, s (As well as in the superclass) s So it looks in the Valid Call subclass, s Finds a DeleteCall method, and so deletes itself. deleting a False Alarm But now, what about deleting a False Alarm call? Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
33
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 33 Call ID Date Time Calling Number Address Create Call Display Details List CallsDeleteCall Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCallDeleteCall Response Specialist 911 Call False Alarm Valid Call Severity CriminalY/N Police Officer Fire Fighter Paramedic handles Vehicle Citizen investigates Weapon Dog * 1 1 * * * ** ** ** So where does a False Alarm instance find some program code for deletions?
34
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 34 A False Alarm 911 Call l Knows it is an instance of the False Alarm class, l (And also of the superclass), looks in the False Alarm subclass So it looks in the False Alarm subclass for a DeleteCall method, doesn’t find one But doesn’t find one there. walks up the hierarchy So now it walks up the hierarchy looks in the superclass, 911 Call. And looks in the superclass, 911 Call. finds a DeleteCall method There it finds a DeleteCall method so it executes that one and happily deletes itself. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
35
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 35 Call ID Date Time Calling Number Address Create Call Display Details List CallsDeleteCall Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCallDeleteCall Response Specialist 911 Call False Alarm Valid Call Severity CriminalY/N Police Officer Fire Fighter Paramedic handles Vehicle Citizen investigates Weapon Dog * 1 1 * * * ** ** ** This Valid Call finds some code right here in this class. This Valid Call finds some code right here in this class. The system walks up the chain looking for some program code to execute. The system walks up the chain looking for some program code to execute.
36
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 36 l So we say that the DeleteCall method in the superclass (911 Call) is available to all instances of all subclasses. overridden DeleteCall same name! in the ValidCall subclass. But it is overridden for instances of the subclass ValidCall by the DeleteCall method ( same name! ) in the ValidCall subclass. l This also applies to any subclasses of the ValidCall class, and all its generations of descendant classes. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
37
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 37 subclass feature We say that a subclass feature (attribute or method)overrides the corresponding superclass feature. subclass actually replaces superclass feature That is, the subclass version of the feature actually replaces the superclass feature for that subclass and all its descendants. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Definition:
38
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 38 l Fig 8.12 (i.e. example in previous slides) n Not polymorphism but inheritance (i.e. overriding) l More examples on overriding see Fig 8.15 n Name attribute in Employee and Person classes Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
39
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 39 Chapter 8: Properties of Objects and Classes Static and Dynamic Binding Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Static and Dynamic Binding
40
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 40 partially-compiled object code, Some O-O languages store the code for the methods as partially-compiled object code, which is then linked into the program at compile time. is a copy This means that there is a copy of the method code inserted into your program when this happens. Static Early Binding This is known as Static or Early Binding bound early Since the method call is bound early to the code it will execute. Chapter 8: Properties of Objects and Classes Static and Dynamic Binding Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Static and Dynamic Binding
41
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 41 fully-compiled binary executable code, Other O-O languages store the code for the methods as fully-compiled binary executable code, stored in the database at compile time which is stored in the database at compile time. Run Time At Run Time (i.e., later), your program’s method call causes a jump to this code, to execute it directly from where it is stored in the database. no copy This means that there is no copy of the method code inserted into your program. DynamicLate Binding, This is known as Dynamic or Late Binding, later at run time l Since the binding of the code to the method call that will invoke it takes place later at run time. Chapter 8: Properties of Objects and Classes Static and Dynamic Binding Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Static and Dynamic Binding
42
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 42 Early (Static) Binding l Means a larger program size, since all the method code is copied into every program. l If you modify a method, it is not available for execution until each program has been recompiled. Late (Dynamic) Binding l The moment you modify a method and store the compiled method code in the database, it is instantly available to any executing program. ( Late Binding is more like true Object- Oriented! ) ( Late Binding is more like true Object- Oriented! ) Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
43
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 43 Polymorphism l Is the ability to write several version of a method (function, subroutine) in different classes of a subclass hierarchy and give them all exactly the same name, and rely on the OO environment to establish which version should be executed, depending on the class of the target object when the method is invoked. l check the third paragraph on page 239 Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
44
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 44 Chapter 8: Properties of Objects and Classes Multiple Inheritance Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance
45
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 45 l Example of Fig 8.16 (mobile home) Paramedic Take a look at the Paramedic on the next slide. “Hospital Staff?” Is she “Hospital Staff?” “Response Specialist?”... Or is she a “Response Specialist?”... Chapter 8: Properties of Objects and Classes Multiple Inheritance Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance
46
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 46 Administrativ e Staff Chapter 8: Properties of Objects and Classes Multiple Inheritance Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance Response Specialist Police Officer Fire Fighter Paramedic Hospital Staff Physicians Patient Care Nurse Nursing Assistant
47
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 47 l Of course, she is both! l Sometimes you find a class that inherits from two (or more) chains of superclasses. ALL both It thus inherits ALL the attributes and behaviors of both chains of superclasses. But this can lead to problems... Chapter 8: Properties of Objects and Classes Multiple Inheritance Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance
48
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 48 Administrativ e Staff Chapter 8: Properties of Objects and Classes Multiple Inheritance Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance Police Officer Fire Fighter Paramedic Physicians Patient Care Nurse Nursing Assistant Response SpecialistGiveRaise Hospital Staff GiveRaise
49
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 49 Hospital Staff Response Specialist each has its own GiveRaise method. You will notice that both the Hospital Staff class and the Response Specialist class each has its own GiveRaise method. l This could be because the two groups have different union agreements, which have different procedures for granting a raise in pay, different program code which means they need different program code to process those raises. l The question is, Which one should a Paramedic instance execute? Chapter 8: Properties of Objects and Classes Multiple Inheritance Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance
50
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 50 l Each OOPL and OODBMS has its own way of resolving this difficulty l Some simply don’t allow multiple inheritance. l Some have a way of forcing one or the other, or both, to execute. l Some force the programmer to choose. roles... Smalltalk does not allow multiple inheritance, but gives a roughly equivalent feature with what it calls roles... Chapter 8: Properties of Objects and Classes Multiple Inheritance Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance
51
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 51 roles Dynamic Typing, Smalltalk’s roles are actually an example of Dynamic Typing, where an object instance can switch from one class (or type) to another, and each of those classes can inherit differently. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
52
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 52 l Now all this inheritance and polymorphism stuff brings us to another powerful feature of objects, not available that is not available with conventional procedural programming. add functionality This is the ability to add functionality to a subroutine or function library, Without having access to the program source code!! Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Object Class Library
53
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 53 It works like this... l If we were to buy an old-fashioned function library, l We can’t modify anything because The vendor doesn’t want us fooling about with their source code, l And maybe blowing it up! “multi-vendor problem” In which case we’re faced with the old “multi-vendor problem” when deciding whose code is to blame. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Object Class Library
54
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 54 object class library, When we buy an object class library, however can We can extend the functionality, By defining a subclass that inherits from one of theirs. new or replacement attributes, We then add any new or replacement attributes, new or replacement methods, And code any new or replacement methods, l And the new class now has: l All the features (attributes and methods) of the original, Plus any we changed or added. Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding.
55
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 55 to the incredible potential for reuse of Object-Oriented program code and of Analysis and Design results as well. Next: Abstract Classes This factor is what gives rise Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. This factor is what gives rise
56
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 56 check out the Response Specialist and subclasses... Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes. In our example of a 911 Call,
57
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 57 Call ID Date Time Calling Number Address Create Call Display Details List CallsDeleteCall Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCallDeleteCall Response Specialist 911 Call False Alarm Valid Call Severity CriminalY/N Police Officer Fire Fighter Paramedic handles Vehicle Citizen investigates Weapon Dog * 1 1 * * * ** ** **
58
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 58 This is the structure we now have: Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes. This is the structure we now have: Response Specialist Police Officer Fire Fighter Paramedic
59
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 59 Are there any other kindsa Response Specialists besides the three kinds we have shown? Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes. The question to ask this time is:
60
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 60 Then we say that the superclass “ Response Specialist” completely covered is completely covered by its subclasses; no Direct Instances, We say it has no Direct Instances, Abstract Class, i.e., it is an Abstract Class, It has no instances other than those provided for it by its subclasses. Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes. l l If the answer is no,
61
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 61 Definition: Abstract Class no direct instances. An Abstract Class is one with no direct instances. But why would we bother? l These are often useful to declare the structure, type or interface (protocol) of an attribute or method, subclasses to provide And allow the subclasses to provide the storage for an attribute, or the code for a method. l This firms up the interface and constrains the developers to stick with it as specified. Next: Components and Wholes. Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes.
62
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 62 Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. 8.5. Aggregation: Components and Wholes.
63
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 63 Chapter 8: Properties of Objects and Classes l 8.0. Review from Chapter 5 - Classes. 8.1. Subclasses and Inheritance. 8.2. More About Inheritance. 8.3. Polymorphism and Overriding. 8.4. Abstract Classes. 8.5. Aggregation: Components and Wholes 8.5. Aggregation: Components and Wholes 8.6. The Difference Between Subclassing and Aggregation.
64
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 64 Whole-Part, Component-Assembly Whole-Part, or Component-Assembly n Container-Contents Collection-Member Collection-Member understanding communication You may sometimes find these useful for making your model a better tool for understanding and communication. talking to the users. l The model can always be built without these - they do not really affect its use for system design, just for talking to the users. Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. l l There are three relationships that sometimes occur in an object model:
65
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 65 Whole-Part Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. Whole-Part
66
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 66 l Taking something apart into its components is a technique we humans often use to understand how something works. l Often we find it improves our understanding to model n A product and its components n A business consists of branches, departments, etc. n A country consists of states, provinces, counties, cities, boroughs, shires, towns, villages, etc. n A college is made up of Faculties, Departments and Sections Whole-Part Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. Whole-Part
67
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 67 “Bill of Materials” In some cases, we can use this for the classical “Bill of Materials” structure, which models the parts needed for a product, and then how the parts also break down into components. “matrix” This structure is an example of a “matrix” structure. matrix A matrix can be viewed as a two-way tree. Whole-Part Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. Whole-Part Part consists of * 1 * *
68
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 68 Container-Contents Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Container-Contents
69
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 69 Container-Contents Container-Contents is a different and less common relationship. l In some situations we may find it helpful to view a relationship as one of these, e.g., n Truck or Aircraft and the Products or Shipments that it carries n An actual shipping container and the goods it holds n A ship, bus or airplane and its passengers n A building and the businesses it houses. Container-Contents Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Container-Contents
70
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 70 Whole-Part Container-Contents The essential difference between Whole-Part and Container-Contents relationships is that: Whole-Part, probably won’t work With Whole-Part, if you take the component away, the assembly (whole) probably won’t work any more s Take a wheel off a car s Take a hand off a clock s Take a leg off a table. Container even without its Contents A Container, however, is still a perfectly good Container, even without its Contents s The jar is still OK even without the “hunny.” Contents OK without the Container And the Contents are perfectly OK without the Container (although the “hunny” might get all over one’s paws!) Container-Contents Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Container-Contents
71
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 71 Collection-Member Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Collection-Member
72
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 72 Collection-Member And lastly, Collection-Member is also a different and relatively uncommon relationship. actual collection: Sometimes we meet an actual collection: n A library full of books n An art gallery n A stamp or jewelry collection n A fleet of trucks, ships or aircraft. l Other times it may help to use this to describe: n A church, club or regiment and its members n An inventory of furniture or equipment n A herd, mob, flock, school or skein of animals. Collection-Member Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Collection-Member
73
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 73 Aggregation Aggregation is a relationship between two classes where the instances of one class are in some way components, members or contents components, members or contents of the instances of the other class. l These are relationships that appear on the class diagram along with all the subclass hierarchies and the regular 1:M and M:M associations Object-Oriented Treasure Hunt (OOTHunt) Exercise 8.2 Have you tried the Object-Oriented Treasure Hunt (OOTHunt) in Exercise 8.2 yet? Chapter 8: Properties of Objects and Classes 8.5. Aggregation. Definition:
74
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 74 End of Chapter 8
75
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 75 End of Chapter 8
76
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 76 End of Chapter 8
77
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 77 End of Chapter 8
78
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 78
79
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 79
80
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 80
81
Copyright 8 2002 Flying Kiwi Productions Inc. flykiwi@home.com 81
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.