Chapter 8 Properties of Objects and Classes

Slides:



Advertisements
Similar presentations
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Lecture 1 Introduction to Computers and Object-
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
1 A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts.
Copyright Flying Kiwi Productions Inc. 1 An Introduction to Object-Oriented Analysis Objects and UML in plain English. Chapter.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Association Class Generalization/Specialization Whole-Part Page More Associations 1.
Systems Analysis and Design in a Changing World, 6th Edition 1 Chapter 4 - Domain Classes.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Copyright Flying Kiwi Productions Inc. 1 An Introduction to Object-Oriented Analysis Objects and UML in plain English. Chapter.
Jerry KotubaSYST39409-Object Oriented Methodologies1 Object Oriented Methodologies Week05/06.
UML Class Diagram Trisha Cummings. What we will be covering What is a Class Diagram? Essential Elements of a UML Class Diagram UML Packages Logical Distribution.
CIS 112 Exam Review. Exam Content 100 questions valued at 1 point each 100 questions valued at 1 point each 100 points total 100 points total 10 each.
Object Oriented Software Development
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
 Week08.  Review Schedule Weeks 8-14  This week o Review last class o Introduce Class Diagrams o ICE-03 Sheridan SYST Engineering Quality Systems.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
CSCI-383 Object-Oriented Programming & Design Lecture 10.
LESSON05 Jerry Kotuba Object Oriented Methodologies 1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Lecture 8&9 11/2/16 Introduction to OO Concepts 1.
Object Oriented Methodologies
5 Systems Analysis and Design in a Changing World, Fourth Edition.
Entity Relationship (E-R) Model
UML Class & Object Diagram I
Object Oriented Programming
Object-Oriented Modeling
Lec-4 : Unified Modeling Language (UML)
Enhanced Entity-Relationship and Object Modeling Objectives
OBJECT ORIENTED CONCEPT
The Object-Oriented Thought Process Chapter 1
Chapter 11 Object-Oriented Design
Chapter 1: Introduction
Class diagram Description
Object-Oriented Programming
Database EER.
Week 4 Object-Oriented Programming (1): Inheritance
OBJECT RELATIONSHIPS, ATTRIBUTES, AND METHODS
Object Oriented Concepts
UML Class Diagrams: Basic Concepts
Basic know how ???.
Engineering Quality Software
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Class Diagram Slide 1.
The Object-Oriented Thought Process Chapter 07
UML Class Diagram.
Data Modeling with Entity Relationship Diagrams (Cont.)
Chapter 20 Object-Oriented Analysis and Design
Chapter 10 Object States and The Statechart Diagram
Java – Inheritance.
ITEC 3220A Using and Designing Database Systems
Fundaments of Game Design
Sampath Jayarathna Cal Poly Pomona
Workshop for Programming And Systems Management Teachers
Applying Use Cases (Chapters 25,26)
Applying Use Cases (Chapters 25,26)
Database EER.
Object Oriented Analysis and Design
Chapter 22 Object-Oriented Systems Analysis and Design and UML
Enhanced Entity-Relationship (EER) Modeling
Use Case Analysis – continued
Enhanced Entity-Relationship (EER) Modeling
Chapter 4 System Modeling.
Introduction to Object-Oriented Software Development
Chapter 7 Inheritance.
Presentation transcript:

Chapter 8 Properties of Objects and Classes

Chapter 8: Properties of Objects and Classes 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.

Review from Chapter 5: Objects and Classes Classes and Classification

8.0. Classes and Classification In the real world We need to consider: Attributes, Behavior, Relationships, and Semantics (meaning).

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.)

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”

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.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

Chapter 8: Properties of Objects and Classes 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.

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.

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.

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.

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. . .

Chapter 8: Properties of Objects and Classes 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.

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. . .

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

Chapter 8: Properties of Objects and Classes 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.

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?)

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 9-1-1 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.

* * * * * * * * * * 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 * *

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.

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!

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.”

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.

* * * * * * * * * * 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 * *

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?

* * * * * * * * * * 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 * *

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.

* * * * * * * * * * 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 * *

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.

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.

Chapter 8: Properties of Objects and Classes 8. 3 Chapter 8: Properties of Objects and Classes 8.3. Polymorphism and Overriding. Multiple Inheritance

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?” . . .

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

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. . .

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

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?

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.

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!!

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.

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.

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

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. . .

* * * * * * * * * * 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 * *

The question to ask this time is: 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 The question to ask this time is: 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. 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.

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.

8.5. Aggregation: Components and Wholes. Chapter 8: Properties of Objects and Classes 8.5. Aggregation: Components and Wholes.

Chapter 8: Properties of Objects and Classes 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.

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.

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

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 *

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.

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

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.

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

End of Chapter 8