properties of objects and classes · for a programmer to use that feature. chapter 8: properties of...

29
1/57 Chapter 8 Chapter 8 Properties of Objects and Classes Properties of Objects and Classes 2/57 Chapter 8: Properties of Objects Chapter 8: Properties of Objects and Classes 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.

Upload: others

Post on 31-May-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

1/57

Chapter 8Chapter 8

Properties of Objects and ClassesProperties of Objects and Classes

2/57

Chapter 8: Properties of Objects Chapter 8: Properties of Objects

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

Page 2: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

3/57

Review from Chapter 5: Objects and Classes

Classes and ClassificationClasses and Classification

4/57

� Attributes,

� Behavior,

� Relationships, and

� Semantics (meaning).

We need to consider:

8.0. Classes and Classification

In the real world

Page 3: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

5/57

Definition:

A classclass is a group of objects with: similar similar propertiesproperties (attributes),

common common behaviorbehavior (operations),

common common relationshipsrelationships to other

objects, and common common semanticssemantics(i.e., meaning.)

8.0. Classes and Classification

In the real world

6/57

� Features not shared by the rest of

the class

� Attributes

� Behavior

� Associations

The key verb is:

Classes and Classification.

Some instances of a class may be Some instances of a class may be

grouped together on the basis of:grouped together on the basis of:

“ISAKINDA”“ISAKINDA”“ISAKINDA”“ISAKINDA”“ISAKINDA”“ISAKINDA”“ISAKINDA”“ISAKINDA”

8.0. Classes and Classification

Subclasses In the real world

Page 4: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

7/57

Hierarchy Diagram

(UMLUML notation)PERSONPERSONPERSONPERSONPERSONPERSONPERSONPERSON

CUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMEREMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEE

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

CANBEACANBEACANBEACANBEA

ISAKINDAISAKINDAISAKINDAISAKINDA

This kind of arrowhead This kind of arrowhead

indicates that this indicates that this

relationship is one of relationship is one of

subclassingsubclassing

8/57

Abstract ClassesAbstract Classes� Such classes have no instances other than

the ones provided for them by their

subclasses

� That is, they have no Direct InstancesDirect Instances� For this reason we call them

Abstract ClassesAbstract Classes

8.0. Classes and ClassificationAdditional Data-world Classes

Page 5: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

9/57

Chapter 8: Properties of Objects Chapter 8: Properties of Objects

and Classesand Classes� 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.

10/57

� A SubclassSubclass is made up of selected instancesselected instances

from another class, the “Parent classParent class” or

“SuperclassSuperclass.”

� A SuperclassSuperclass includes ALLALL the instances of

the Subclass, plus possibly more as well.

�� InheritanceInheritance is when a subclass instance, in in

additionaddition to the attributes and behavior it has by virtue of being in the subclass,

also has allall the attributes and behaviorthe attributes and behavior

that instances of the superclass have.

Chapter 8: Properties of Objects and Classes

���� 8.1. Subclasses and Inheritance.

Page 6: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

11/57

� To be considered truly Otruly O--OO,, a language, database, etc. must support:

Objects

Classes

Inheritance

Polymorphism (to be defined shortly)

� We can define supportingsupporting a feature to mean

that the language makes it ““easy and convenienteasy 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:

12/57

� Another thing we can do with subclasses is show

more detail about associationsassociations on our diagram.

� For instance, in most companies, only managers

can hire and fire.

� In other words, only certain kindsakindsa employees can do certain tasks.

� Thus we are able to show that some associations

affect only a subclassonly a subclass, and not every instance.

Chapter 8: Properties of Objects and Classes

���� 8.1. Subclasses and Inheritance.

Page 7: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

13/57

Union

Chapter 8: Properties of Objects and Classes

���� 8.1. Subclasses and Inheritance.

Company

Vehicle

Regular

Employee

Manager

Employee

isakinda canbea

supervisessupervises

drives belongs to

Next: More About Inheritance. . .Next: More About Inheritance. . .

**11

**

11 **11

14/57

Chapter 8: Properties of Objects Chapter 8: Properties of Objects

and Classesand Classes� 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.

Page 8: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

15/57

� A Payables Account is a special case special case of an Account.

� What makes this AccountAccount different (i.e., specialized) (i.e., specialized) is

that it is a PayablesPayables Account,

� And that other one over there is a ReceivablesReceivables

Account.

� Thus, the Payables and the Receivables subclasses

may be viewed as specializingspecializing the Account class.

� A subclass is said to specializespecialize its superclass(es) by

including only certain of the superclass instancesonly certain of the superclass instances.

Now let’s take another look at inheritance. . .Now let’s take another look at inheritance. . .

Chapter 8: Properties of Objects and Classes

����8.2. More About Inheritance.

Generalization and SpecializationGeneralization and Specialization

16/57

Hierarchy Diagram

(UMLUML notation)PERSONPERSONPERSONPERSONPERSONPERSONPERSONPERSON

CUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMERCUSTOMEREMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEEEMPLOYEE

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

CANBEACANBEACANBEACANBEA

ISAKINDAISAKINDAISAKINDAISAKINDA

This kind of arrowhead to This kind of arrowhead to

indicate that this indicate that this

relationship is one of relationship is one of

subclassingsubclassing

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.

Page 9: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

17/57

Chapter 8: Properties of Objects Chapter 8: Properties of Objects

and Classesand Classes� 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.

18/57

� 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. Polymorphism and Overriding.

Page 10: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

19/57

On the next slide,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” “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.

20/57

Call ID

Date

Time

Calling Number

Address

Create Call

Display Details

List Calls

DeleteCallDeleteCall

Problem

Crime/Fire/Acdnt/Poison

Date/Time Dispatched

Date/Time Completed

DispatchCall

CompleteCall

DeleteCallDeleteCall

Response

Specialist

911 Call

False Alarm

Valid CallSeverity

CriminalY/N

DeleteCallDeleteCall

Police

Officer

Fire

FighterParamedic

handles

Vehicle

Citizen

investigates

Weapon

Dog

11

**

**

**

** **

****

** **

CANBEACANBEAISAKINDAISAKINDA

Page 11: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

21/57

� You can see that the amount of work that has to

be done deleting a call

depends on the kindakinda call.

� A False AlarmFalse Alarm needs some program code that will disconnect it from the investigating officer as

well as the offending citizen.

� A Valid CallValid 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.

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

22/57

� Now you see that we need at least two different

pieces of program code to delete these two

kindsakindsa 911 calls.

� But I have shown them both with the same same

name!!name!!

� This would never work in regular “procedural”

programming,

� But it can be made to work here, and

this is the power of objects!this is the power of objects!

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Page 12: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

23/57

� Object-Oriented languages allow us to define two

functions (i.e., methods) with the same name

� Provided they are in different classesdifferent classes

but they must be subclasses and superclasses.

� Whichever kindakinda 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, “The object knows its own type,

so it knows what to do.”so it knows what to do.”

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

24/57

� And what about a 911 Call instance that needs to

be cancelled before it is even evaluated 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.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 DeleteCallDeleteCallmethod in the False Alarm class.

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Page 13: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

25/57

Call ID

Date

Time

Calling Number

Address

Create Call

Display Details

List Calls

DeleteCallDeleteCall

Problem

Crime/Fire/Acdnt/Poison

Date/Time Dispatched

Date/Time Completed

DispatchCall

CompleteCall

DeleteCallDeleteCall

Response

Specialist

911 Call

False Alarm

Valid CallSeverity

CriminalY/N

Police

Officer

Fire

FighterParamedic

handles

Vehicle

Citizen

investigates

Weapon

Dog

11

**

**

**

** **

****

** **

26/57

� When an Unevaluated 911 Call is to be deleted,

It knows it is an instance onlyonly of the superclass, so it

looks in there for some code to execute.

Of course it finds a DeleteCallDeleteCall 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 deleting a False Alarm call?

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Page 14: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

27/57

Call ID

Date

Time

Calling Number

Address

Create Call

Display Details

List Calls

DeleteCallDeleteCall

Problem

Crime/Fire/Acdnt/Poison

Date/Time Dispatched

Date/Time Completed

DispatchCall

CompleteCall

DeleteCallDeleteCall

Response

Specialist

911 Call

False Alarm

Valid CallSeverity

CriminalY/N

Police

Officer

Fire

FighterParamedic

handles

Vehicle

Citizen

investigates

Weapon

Dog

11

**

**

**

** **

****

** **

So where does a False Alarm So where does a False Alarm

instance find some programinstance find some program

code for deletions?code for deletions?

28/57

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 looks in the False Alarm subclass for a

DeleteCall method,

� But doesn’t find one doesn’t find one there.

� So now it walks up the hierarchywalks up the hierarchy

� And looks in the superclass,looks in the superclass, 911 Call.911 Call.

� There it finds a finds a DeleteCall DeleteCall method method so it executes

that one and happily deletes itself.

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Page 15: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

29/57

Call ID

Date

Time

Calling Number

Address

Create Call

Display Details

List Calls

DeleteCallDeleteCall

Problem

Crime/Fire/Acdnt/Poison

Date/Time Dispatched

Date/Time Completed

DispatchCall

CompleteCall

DeleteCallDeleteCall

Response

Specialist

911 Call

False Alarm

Valid CallSeverity

CriminalY/N

Police

Officer

Fire

FighterParamedic

handles

Vehicle

Citizen

investigates

Weapon

Dog

11

**

**

**

** **

****

** **This Valid Call finds some This Valid Call finds some

code right here in this classcode right here in this class..

The system walks upThe system walks up

the chain looking forthe chain looking for

some program codesome program code

to execute.to execute.

30/57

� So we say that the DeleteCall method in the

superclass (911 Call) is available to all instances

of all subclasses.

� But it is overriddenoverridden for instances of the subclass ValidCall by the DeleteCallDeleteCallmethod

(same name!same name!) in the in the ValidCallValidCall subclass. subclass.

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

Page 16: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

31/57

We say that a subclass feature subclass feature (attribute or method)

overridesoverridesthe corresponding superclass feature.

That is, the subclasssubclass version of the feature

actually replaces actually replaces the superclass feature superclass feature

for that subclass and all its for that subclass and all its

descendants.descendants.

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Definition:

32/57

Chapter 8: Properties of Objects and ClassesChapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Multiple InheritanceMultiple Inheritance

Page 17: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

33/57

� Take a look at the ParamedicParamedic on the next

slide.

� Is she “Hospital Staff?”“Hospital Staff?”

� Or is she a “Response Specialist?” . . .“Response Specialist?” . . .

Chapter 8: Properties of Objects and ClassesChapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Multiple InheritanceMultiple Inheritance

34/57

Administrativ

e

Staff

Chapter 8: Properties of Objects and ClassesChapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Multiple InheritanceMultiple Inheritance

Response

Specialist

Police

Officer

Fire

FighterParamedicParamedic

Hospital Staff

PhysiciansPatient

Care

Nurse Nursing

Assistant

Page 18: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

35/57

� Of course, she is both!

� Sometimes you find a class that inherits

from two (or more) chains of superclasses.

� It thus inherits ALLALL the attributes and

behaviors of bothboth chains of superclasses.

But this can lead to problems. . .But this can lead to problems. . .

Chapter 8: Properties of Objects and ClassesChapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Multiple InheritanceMultiple Inheritance

36/57

Administrativ

e

Staff

Chapter 8: Properties of Objects and ClassesChapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Multiple InheritanceMultiple Inheritance

Police

Officer

Fire

FighterParamedic

PhysiciansPatient

Care

Nurse Nursing

Assistant

Response

Specialist

GiveRaiseGiveRaise

Hospital Staff

GiveRaiseGiveRaise

Page 19: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

37/57

� You will notice that both the Hospital StaffHospital Staff class

and the Response SpecialistResponse Specialist class each has its own each has its own

GiveRaise method.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 different program code to

process those raises.

� The question is,

Which one should a Paramedic instance execute?Which one should a Paramedic instance execute?

Chapter 8: Properties of Objects and ClassesChapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Multiple InheritanceMultiple Inheritance

38/57

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

Chapter 8: Properties of Objects and ClassesChapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Multiple InheritanceMultiple Inheritance

Page 20: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

39/57

� Now all this inheritance and polymorphism stuff

brings us to another powerful feature of objects,

that is not available not available with conventional

procedural programming.

� This is the ability to add functionality add functionality to a subroutine or function library,

Without having access to the Without having access to the

program source code!!program source code!!

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

40/57

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 The vendor doesn’t want us fooling about

with their source code,with their source code,� And maybe blowing it up!

� In which case we’re faced with the old “multi“multi--vendor vendor

problem” problem” when deciding whose code is to blame.

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

Page 21: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

41/57

� When we buy an object class library, object class library, however

� We cancan extend the functionality,

By defining a subclass that inherits By defining a subclass that inherits

from one of theirs.from one of theirs.� We then add any new or replacement attributes,new or replacement attributes,

� And code any new or replacement methods,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.Plus any we changed or added.

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

42/57

to the incredible potential for to the incredible potential for reuse of Objectreuse of Object--Oriented Oriented

program codeprogram code

and of Analysis and Design and of Analysis and Design results as well.results as well.

Next: Abstract ClassesNext: Abstract Classes

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

This factor is what gives riseThis factor is what gives rise

Chapter 8: Properties of Objects and Classes

����8.3. Polymorphism and Overriding.

This factor is what gives riseThis factor is what gives rise

Page 22: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

43/57

check out thecheck out the

Response SpecialistResponse Specialist

and subclasses. . . and subclasses. . .

Chapter 8: Properties of Objects and Classes

���� 8.4. Abstract Classes.

In our example of a 911 Call,In our example of a 911 Call,

44/57

Call ID

Date

Time

Calling Number

Address

Create Call

Display Details

List Calls

DeleteCallDeleteCall

Problem

Crime/Fire/Acdnt/Poison

Date/Time Dispatched

Date/Time Completed

DispatchCall

CompleteCall

DeleteCallDeleteCall

Response

Specialist

911 Call

False Alarm

Valid CallSeverity

CriminalY/N

Police

Officer

Fire

FighterParamedic

handleshandles

Vehicle

Citizen

investigatesinvestigates

Weapon

Dog

11

**

**

**

** **

****

** **

Page 23: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

45/57

Chapter 8: Properties of Objects and Classes

���� 8.4. Abstract Classes.

This is the structure we now have:This is the structure we now have:

Response

Specialist

Police

Officer

Fire

FighterParamedic

The question to ask this time is:Are there any other Are there any other

kindsa kindsa Response Specialists Response Specialists besides the three kinds we have shown?besides the three kinds we have shown?

46/57

Then we say that the superclass

““Response Specialist”Response Specialist”

is completely covered completely covered by its subclasses;

�We say it has no Direct Instances,no Direct Instances,

� i.e., it is an Abstract Class, Abstract Class, It has no instances other than those It has no instances other than those

provided for it by its subclasses.provided for it by its subclasses.

Chapter 8: Properties of Objects and Classes

���� 8.4. Abstract Classes.

�If the answer is no,

Page 24: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

47/57

Definition:

� An Abstract Class Abstract Class is one with no direct instances.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 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.Next: Components and Wholes.

Chapter 8: Properties of Objects and Classes

���� 8.4. Abstract Classes.

48/57

Chapter 8: Properties of Objects and Classes

����8.5. Aggregation: Components 8.5. Aggregation: Components

and Wholes.and Wholes.

Page 25: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

49/57

Chapter 8: Properties of Objects Chapter 8: Properties of Objects

and Classesand 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 Wholes8.5. Aggregation: Components and Wholes

����8.6. The Difference Between Subclassing

and Aggregation.

50/57

��WholeWhole--Part, Part, or ComponentComponent--AssemblyAssembly

��ContainerContainer--ContentsContents

��CollectionCollection--MemberMember

� You may sometimes find these useful for making

your model a better tool for understandingunderstanding and

communicationcommunication.

� The model can always be built without these -

they do not really affect its use for system design,

just for talking to the users.talking to the users.

Chapter 8: Properties of Objects and Classes

����8.5. Aggregation: Components and Wholes.

�There are three relationships that sometimes occur

in an object model:

Page 26: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

51/57

� 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. Aggregation: Components and Wholes.

WholeWhole--PartPart

52/57

� In some cases, we can use this for the classical

“Bill of Materials” “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” “matrix” structure.� A matrixmatrix can be viewed as a two-way tree.

Chapter 8: Properties of Objects and Classes

����8.5. Aggregation: Components and Wholes.

WholeWhole--PartPart

Part

consists ofconsists of

**

**

Page 27: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

53/57

�� ContainerContainer--Contents 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.

Chapter 8: Properties of Objects and Classes

����8.5. Aggregation:

ContainerContainer--ContentsContents

54/57

� The essential difference between WholeWhole--Part Part and

ContainerContainer--ContentsContents relationships is that:

� With WholeWhole--Part, Part, if you take the component away, the

assembly (whole) probably won’t work 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 ContainerContainer, however, is still a perfectly good

Container, even without its Contents even without its Contents

The jar is still OK even without the “water in side.”

� And the ContentsContents are perfectly OK without the Container OK without the Container

Chapter 8: Properties of Objects and Classes

����8.5. Aggregation:

ContainerContainer--ContentsContents

Page 28: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

55/57

� And lastly, CollectionCollection--MemberMember is also a different and relatively uncommon relationship.

� Sometimes we meet an actual collection: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:

CollectionCollection--MemberMember

56/57

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

� These are relationships that appear on the

class diagram along with all the subclass

hierarchies and the regular 1:M and M:M

associations

Chapter 8: Properties of Objects and Classes

����8.5. Aggregation.

Definition:

Page 29: Properties of Objects and Classes · for a programmer to use that feature. Chapter 8: Properties of Objects and Classes 8.1. Subclasses and Inheritance. Definition of Object-Oriented:

57/57

End of Chapter 8End of Chapter 8