Download presentation
Presentation is loading. Please wait.
1
Chapter 8 Properties of Objects and Classes
2
Chapter 8: Properties of Objects and Classes
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.
3
Review from Chapter 5: Objects and Classes
Classes and Classification
4
8.0. Classes and Classification In the real world
We need to consider: Attributes, Behavior, Relationships, and Semantics (meaning).
5
8.0. Classes and Classification In the real world
Definition: 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.)
6
8.0. Classes and Classification Subclasses In the real world
Some instances of a class may be grouped together on the basis of: Features not shared by the rest of the class Attributes Behavior Associations The key verb is: “ISAKINDA”
7
Hierarchy Diagram (UML notation)
PERSON Name, Address Phone, Sex Date of Birth ChangeAddress EnquireDOB&Sex CANBEA ISAKINDA CUSTOMER EMPLOYEE Balance O/Due 30, 60, 90 Credit Rating Date Paid CheckCrRating AgeBalances SIN Marital Status No. of Dependants Date Hired Wage Rate GiveRaise CalcMonthPay This kind of arrowhead indicates that this relationship is one of subclassing
8
8.0. Classes and Classification Additional Data-world Classes
Abstract Classes Such classes have no instances other than the ones provided for them by their subclasses That is, they have no Direct Instances For this reason we call them Abstract Classes
9
Chapter 8: Properties of Objects and Classes
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.
10
Chapter 8: Properties of Objects and Classes 8. 1
Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. A Subclass is made up of selected instances from another class, the “Parent class” or “Superclass.” A Superclass includes ALL the instances of the Subclass, plus possibly more as well. Inheritance is when a subclass instance, in addition to the attributes and behavior it has by virtue of being in the subclass, also has all the attributes and behavior that instances of the superclass have.
11
Definition of Object-Oriented:
Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented: To be considered truly O-O, a language, database, etc. must support: Objects Classes Inheritance Polymorphism (to be defined shortly) We can define supporting a feature to mean that the language makes it “easy and convenient” for a programmer to use that feature.
12
Chapter 8: Properties of Objects and Classes 8. 1
Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Another thing we can do with subclasses is show more detail about associations on our diagram. For instance, in most companies, only managers can hire and fire. In other words, only certain kindsa employees can do certain tasks. Thus we are able to show that some associations affect only a subclass, and not every instance.
13
Chapter 8: Properties of Objects and Classes 8. 1
Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Employee isakinda canbea Manager supervises Regular Employee 1 * * 1 drives belongs to * 1 Company Vehicle Union Next: More About Inheritance. . .
14
Chapter 8: Properties of Objects and Classes
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.
15
Generalization and Specialization
Chapter 8: Properties of Objects and Classes 8.2. More About Inheritance. Generalization and Specialization A Payables Account is a special case of an Account. What makes this Account different (i.e., specialized) is that it is a Payables Account, And that other one over there is a Receivables Account. Thus, the Payables and the Receivables subclasses may be viewed as specializing the Account class. 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. . .
16
Hierarchy Diagram (UML notation)
PERSON All these attributes and behaviors are inherited as is by both subclasses Name, Address Phone, Sex Date of Birth ChangeAddress EnquireDOB&Sex Each subclass then adds attributes and behaviors that it needs but the other one doesn’t. CANBEA ISAKINDA CUSTOMER EMPLOYEE Balance O/Due 30, 60, 90 Credit Rating Date Paid CheckCrRating() AgeBalances() SIN Marital Status No. of Dependants Date Hired Wage Rate GiveRaise() CalcMonthPay() This kind of arrowhead to indicate that this relationship is one of subclassing
17
Chapter 8: Properties of Objects and Classes
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.
18
Chapter 8: Properties of Objects and Classes 8. 3
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Polymorphism Poly = many Morph = shape Polymorphism means “occurring in various forms.” Along with Inheritance, polymorphism is one of the major keys to the power of objects. (A “primary key”, perhaps?)
19
Chapter 8: Properties of Objects and Classes 8. 3
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. On the next slide, You will see a class diagram for handling emergency calls. Study it well. Take particular note of the “DeleteCall” method that appears in the superclass “911 Call” as well as in both subclasses.
20
* * * * * * * * * * 1 1 handles Citizen Response Specialist Fire
911 Call handles Response Specialist Call ID Date Time Calling Number Address Create Call Display Details List Calls DeleteCall * Police Officer Fire Fighter Paramedic 1 * 1 investigates * ISAKINDA CANBEA * Vehicle False Alarm Severity CriminalY/N DeleteCall Valid Call * * Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCall DeleteCall Weapon * * Dog * *
21
depends on the kinda call.
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. You can see that the amount of work that has to be done deleting a call depends on the kinda call. A False Alarm needs some program code that will disconnect it from the investigating officer as well as the offending citizen. A Valid Call needs program code to disconnect it from: Many Response Specialists of various kinds, Many Citizens, and this code it can share with the False Alarm calls, Many Vehicles, Weapons and Dogs.
22
this is the power of objects!
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Now you see that we need at least two different pieces of program code to delete these two kindsa 911 calls. But I have shown them both with the same name!! This would never work in regular “procedural” programming, But it can be made to work here, and this is the power of objects!
23
but they must be subclasses and superclasses.
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Object-Oriented languages allow us to define two functions (i.e., methods) with the same name Provided they are in different classes but they must be subclasses and superclasses. Whichever kinda 911 Call we are handling, the system will choose the right version of the method to suit that particular Call instance. We say that “The object knows its own type, so it knows what to do.”
24
This one is an instance of the superclass, But not of either subclass.
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. 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? This one is an instance of the superclass, But not of either subclass. 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. This gives us the diagram on the next slide, with no DeleteCall method in the False Alarm class.
25
* * * * * * * * * * 1 1 handles Citizen Response Specialist Fire
911 Call handles Citizen Response Specialist Call ID Date Time Calling Number Address Create Call Display Details List Calls DeleteCall * Police Officer Fire Fighter Paramedic 1 * 1 investigates * * Vehicle False Alarm Severity CriminalY/N Valid Call * * Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCall DeleteCall Weapon * * Dog * *
26
When an Unevaluated 911 Call is to be deleted,
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. When an Unevaluated 911 Call is to be deleted, It knows it is an instance only of the superclass, so it looks in there for some code to execute. Of course it finds a DeleteCall method, so it executes it and deletes the call. When a Valid 911 Call is to be deleted, It knows it is an instance in the Valid Call subclass, (As well as in the superclass) So it looks in the Valid Call subclass, Finds a DeleteCall method, and so deletes itself. But now, what about deleting a False Alarm call?
27
* * * * * * * * * * 1 1 So where does a False Alarm
911 Call handles Citizen Response Specialist Call ID Date Time Calling Number Address Create Call Display Details List Calls DeleteCall * Police Officer Fire Fighter Paramedic 1 * 1 investigates * * Vehicle False Alarm Severity CriminalY/N Valid Call * * Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCall DeleteCall Weapon * * So where does a False Alarm instance find some program code for deletions? Dog * *
28
Chapter 8: Properties of Objects and Classes 8. 3
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. A False Alarm 911 Call Knows it is an instance of the False Alarm class, (And also of the superclass), So it looks in the False Alarm subclass for a DeleteCall method, But doesn’t find one there. So now it walks up the hierarchy And looks in the superclass, 911 Call. There it finds a DeleteCall method so it executes that one and happily deletes itself.
29
* * * * * * * * * * 1 1 Citizen handles Response Specialist
911 Call handles Response Specialist Call ID Date Time Calling Number Address Create Call Display Details List Calls DeleteCall * The system walks up the chain looking for some program code to execute. Police Officer Fire Fighter Paramedic 1 * 1 investigates * * Vehicle False Alarm Severity CriminalY/N Valid Call * * Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCall DeleteCall Weapon * * This Valid Call finds some code right here in this class. Dog * *
30
Chapter 8: Properties of Objects and Classes 8. 3
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. So we say that the DeleteCall method in the superclass (911 Call) is available to all instances of all subclasses. But it is overridden for instances of the subclass ValidCall by the DeleteCall method (same name!) in the ValidCall subclass. This also applies to any subclasses of the ValidCall class, and all its generations of descendant classes.
31
for that subclass and all its descendants.
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Definition: We say that a subclass feature (attribute or method) overrides the corresponding superclass feature. That is, the subclass version of the feature actually replaces the superclass feature for that subclass and all its descendants.
32
Chapter 8: Properties of Objects and Classes 8. 3
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance
33
Take a look at the Paramedic on the next slide.
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance Take a look at the Paramedic on the next slide. Is she “Hospital Staff?” Or is she a “Response Specialist?” . . .
34
Chapter 8: Properties of Objects and Classes 8. 3
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance Hospital Staff Response Specialist Patient Care Physicians Administrative Staff Nurse Nursing Assistant Fire Fighter Police Officer Paramedic
35
But this can lead to problems. . .
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance Of course, she is both! Sometimes you find a class that inherits from two (or more) chains of superclasses. It thus inherits ALL the attributes and behaviors of both chains of superclasses. But this can lead to problems. . .
36
Chapter 8: Properties of Objects and Classes 8. 3
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance Response Specialist GiveRaise Hospital Staff GiveRaise Patient Care Physicians Administrative Staff Nurse Nursing Assistant Fire Fighter Police Officer Paramedic
37
Which one should a Paramedic instance execute?
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance You will notice that both the Hospital Staff class and the Response Specialist class each has its own GiveRaise method. This could be because the two groups have different union agreements, which have different procedures for granting a raise in pay, which means they need different program code to process those raises. The question is, Which one should a Paramedic instance execute?
38
Each OOPL and OODBMS has its own way of resolving this difficulty
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance Each OOPL and OODBMS has its own way of resolving this difficulty Some simply don’t allow multiple inheritance. Some have a way of forcing one or the other, or both, to execute. Some force the programmer to choose.
39
Without having access to the program source code!!
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Now all this inheritance and polymorphism stuff brings us to another powerful feature of objects, that is not available with conventional procedural programming. This is the ability to add functionality to a subroutine or function library, Without having access to the program source code!!
40
The vendor doesn’t want us fooling about with their source code,
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. It works like this. . . If we were to buy an old-fashioned function library, We can’t modify anything because The vendor doesn’t want us fooling about with their source code, And maybe blowing it up! In which case we’re faced with the old “multi-vendor problem” when deciding whose code is to blame.
41
By defining a subclass that inherits from one of theirs.
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. When we buy an object class library, however We can extend the functionality, By defining a subclass that inherits from one of theirs. We then add any new or replacement attributes, And code any new or replacement methods, And the new class now has: All the features (attributes and methods) of the original, Plus any we changed or added.
42
to the incredible potential for reuse of Object-Oriented program code
Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. This factor is what gives rise to the incredible potential for reuse of Object-Oriented program code and of Analysis and Design results as well. Next: Abstract Classes
43
Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes.
In our example of a 911 Call, check out the Response Specialist and subclasses. . .
44
* * * * * * * * * * 1 1 handles Citizen Response Specialist Fire
911 Call handles Citizen Response Specialist Call ID Date Time Calling Number Address Create Call Display Details List Calls DeleteCall * Police Officer Fire Fighter Paramedic 1 * 1 investigates * * Vehicle False Alarm Severity CriminalY/N Valid Call * * Problem Crime/Fire/Acdnt/Poison Date/Time Dispatched Date/Time Completed DispatchCall CompleteCall DeleteCall Weapon * * Dog * *
45
The question to ask this time is:
Chapter 8: Properties of Objects and Classes Abstract Classes. This is the structure we now have: Response Specialist Police Officer Fire Fighter Paramedic The question to ask this time is: Are there any other kindsa Response Specialists besides the three kinds we have shown?
46
Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes.
If the answer is no, Then we say that the superclass “Response Specialist” is completely covered by its subclasses; We say it has no Direct Instances, i.e., it is an Abstract Class, It has no instances other than those provided for it by its subclasses.
47
Chapter 8: Properties of Objects and Classes 8.4. Abstract Classes.
Definition: An Abstract Class is one with no direct instances. But why would we bother? These are often useful to declare the structure, type or interface (protocol) of an attribute or method, And allow the subclasses to provide the storage for an attribute, or the code for a method. This firms up the interface and constrains the developers to stick with it as specified. Next: Components and Wholes.
48
8.5. Aggregation: Components and Wholes.
Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes.
49
Chapter 8: Properties of Objects and Classes
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.
50
Whole-Part, or Component-Assembly Container-Contents Collection-Member
Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. There are three relationships that sometimes occur in an object model: Whole-Part, or Component-Assembly Container-Contents Collection-Member You may sometimes find these useful for making your model a better tool for understanding and communication. The model can always be built without these - they do not really affect its use for system design, just for talking to the users.
51
Chapter 8: Properties of Objects and Classes 8. 5
Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. Whole-Part Taking something apart into its components is a technique we humans often use to understand how something works. Often we find it improves our understanding to model A product and its components A business consists of branches, departments, etc. A country consists of states, provinces, counties, cities, boroughs, shires, towns, villages, etc. A college is made up of Faculties, Departments and Sections
52
Chapter 8: Properties of Objects and Classes 8. 5
Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes. Whole-Part 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. This structure is an example of a “matrix” structure. A matrix can be viewed as a two-way tree. 1 * consists of * Part *
53
Chapter 8: Properties of Objects and Classes 8. 5
Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Container-Contents Container-Contents is a different and less common relationship. In some situations we may find it helpful to view a relationship as one of these, e.g., Truck or Aircraft and the Products or Shipments that it carries An actual shipping container and the goods it holds A ship, bus or airplane and its passengers A building and the businesses it houses.
54
And the Contents are perfectly OK without the Container
Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Container-Contents The essential difference between Whole-Part and Container-Contents relationships is that: With Whole-Part, if you take the component away, the assembly (whole) probably won’t work any more Take a wheel off a car Take a hand off a clock Take a leg off a table. A Container, however, is still a perfectly good Container, even without its Contents The jar is still OK even without the “water in side.” And the Contents are perfectly OK without the Container
55
Chapter 8: Properties of Objects and Classes 8. 5
Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Collection-Member And lastly, Collection-Member is also a different and relatively uncommon relationship. Sometimes we meet an actual collection: A library full of books An art gallery A stamp or jewelry collection A fleet of trucks, ships or aircraft. Other times it may help to use this to describe: A church, club or regiment and its members An inventory of furniture or equipment.
56
Chapter 8: Properties of Objects and Classes 8.5. Aggregation.
Definition: Aggregation is a relationship between two classes where the instances of one class are in some way components, members, or contents of the instances of the other class. These are relationships that appear on the class diagram along with all the subclass hierarchies and the regular 1:M and M:M associations
57
End of Chapter 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.