Subtypes Copyright © 1999 Patrick McDermott UC Berkeley Extension
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
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
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
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
Teacher TEACHER Name Address Telephone Salary GetPaid() GetHired() Quit() PartTime Hours Contract() Block Arrow:
Instead of Inheritance Sometimes you DO want to inherit, sometimes you don’t. or just some… Delegation Composition Aggregation Says can use to avoid violating LSP: BUT it just avoids inheritance. In the end, they look the same in the database…
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
Employee EMPLOYEE Name Address Telephone GetHired() GetPaid() Transfer() Quit() BlueCollar HourlyRate Union GetPaid() PayUnion() Grieve() WhiteCollar Salary GetStock() Block Arrow:
Should You Subclass? Different times Different users Different behavior If overlap a lot, don’t subclass
A Difference in Degree, or Kind? ’s fields 1 Record 2 Record Types 3 Records ’s fields ’s fields common fields ’s fields ’s fields common fields Used Only By s Used Only By s Or Type Type Plain Type
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