Inheritance vs Composition Picking the right tool
Inheritance – Technical mechanism for code reuse – Specific guarantee : Is-A Relationship Apple is-a Fruit Composition – Technical mechanism for code reuse – Indicate Has-A Relationship Car has-a Engine
Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance
Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance
Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance
Is-A Gurantee DerivedCylinder can be treated like a Circle!!!
Composed Cylinder Have Circle, want to make Cylinder… – With Composition
Composed Cylinder Can't treat Cylinder as a Circle
Other Composition +'s Can have multiple members of same type – Triangle has three Points
Type Explosion Want to represent orders at a company – , Fax, Phone, InPerson orders – Credit, Check, Cash payment options
Type Explosion Option 1 : Inheritance Order class -Sub classes Order, FaxOrder, PhoneOrder… - Order subclasses: CreditOrder, CheckOrder, CashOrder… -FaxOrderSubClasses: FaxCreditOrder, FaxCheckOrder, FaxCashOrder… …
Type Explosion Option 2 : Composition OrderType class -subtypes: , Phone, … PaymentType class -subtypes: Credit, Check, … Order class -Members : OrderType, PaymentType
Type Explosion Option 3 : Multiple Inheritance OrderType class -subtypes: , Phone, … PaymentType class -subtypes: Credit, Check, … CreditOrder – multiple inheritance from , Credit CheckOrder – multiple inheritance from , Check …
Google says Composition is often more appropriate than inheritance. When using inheritance, make it public… Try to restrict use of inheritance to the "is-a" case: Bar subclasses Foo if it can reasonably be said that Bar "is a kind of" Foo.
Designing with Inheritance 1.Identify existing Is-A relationships – Use Inheritance to eliminate duplication 2.Identify remaining duplicate information 3.Look to introduce new parent classes – Where appropriate
Inheritance Design Sample Want to represent following worker types: – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: salaried, but gets bonus of 5% of sales Need name, title, salary, amount of sales, and method to calculate the amount for paycheck
1) Existing Is-A relationship: – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need name, title, salary, amount of sales, and method to calculate the amount for paycheck
1B) Inherit & Eliminate – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck
2) Look for other duplication – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: salaried, but gets bonus of 5% of sales Need amount of sales, and method to calculate the amount for paycheck
3) "Discover" common parent class – Employee Need name, title, method to calculate amount for paycheck – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck
3B) Inherit and eliminate – Employee Need name, title, method to calculate amount for paycheck – Hourly Need hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck
Quick Challenge Sketch a quick UML diagram for classes below – Don't worry about behaviors, just state – Add classes where appropriate DVD Title, Release year, Director, Price CD Title, Release year, Genre, Price Book Title, Isbn, Price EBook Title, Isbn, Format (PDF/Kindle/etc…), Price