Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension [email protected].

12
Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension [email protected]

Transcript of Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension [email protected].

Page 1: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Subtypes

Copyright © 1999 Patrick McDermott

UC BerkeleyExtension

[email protected]

Page 2: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Inheritance CRC Cards: Superclass/Subclass ERD: Supertype/Subtype Relational: N/A Database: Three Variations Programmer: Base/Derived UML: Generalization/Specialization Etc., etc., etc.…: Is-a, Root/Branch/Leaf,

Extension

Page 3: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Types of Relationship

• IS-A: Generalization• HAS-A: Aggregation• IS-Made-From: Composition

– “A form of aggregation with strong ownership and coincident lifetime of parts by the whole.”– Rumbaugh, Reference, p. 226

– I don’t bother distinguishing from Aggregation

• USES: Association– Default; those that aren’t one of the others– Usually Name according to Use

Page 4: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Subtype

• A subtype is a subtype of another entity (supertype) where occurrences of both designate the same object in the real world, but the subtype has additional, more specific properties.

• The primary key of a subtype should be the same as the primary key of the supertype

Page 5: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Analogies• Base/Derived

– Generalization: General-Specific– Genus Species

• Supertype-Type-Subtype– Superclass-Subclass

• Tree– Root-Branch-Leaf

• Inheritance– Parent-Child [Cloned]

• “Is a”– “A Kind of”

Pietr Brueg(h)el (the Elder)The Tower of Babel, 1563

Page 6: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Teacher

TEACHER

NameAddressTelephoneSalary

GetPaid()GetHired()Quit()

PartTime

Hours

Contract()

Block Arrow:

Page 7: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Instead of Inheritance• Sometimes you DO want to inherit,

sometimes you don’t. or just some…

DelegationCompositionAggregation• Says can use to avoid violating LSP: BUT it

just avoids inheritance. In the end, they look the same in the database…

Page 8: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Super Type Confusion• More attributes means Fewer Objects• Supertype is subset of type...• If you understand the set theory from which

it comes, no (new) confusion

This has something more, else it would be in

the larger category

Page 9: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Employee

EMPLOYEE

NameAddressTelephoneGetHired()GetPaid()Transfer()Quit()

BlueCollar

HourlyRateUnionGetPaid()PayUnion()Grieve()

WhiteCollar

Salary

GetStock()

Block Arrow:

Page 10: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

Should You Subclass?Different timesDifferent usersDifferent behavior

• If overlap a lot, don’t subclass

Page 11: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

A Difference in Degree, or Kind?

c a’s fields

1 Record

2 Record Types

3 Records

a a’s fields

b b’s fields

b’s fieldscommon fields

common fields

common fields

c

a’s fields b’s fields

common fields

Used Only By as

Used Only By bs

Or

Type a Type b

Plain Type

Page 12: Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension pmcdermott@msn.com.

K.I.S.S.• There must be a Business Need

– A Distinction without Difference– A difference is a difference only if it makes a

difference• Analogies aren’t exact• Too many levels can be confusing• Multiple Inheritance can be confusing• When in doubt, leave it out

– Anything that can be done with inheritance can be done without