222 CHAPTER12. OBJECT-ORIENTEDDESIGN
Froma designstandpoint,encapsulationalsoprovidesa criticalserviceofseparatingtheconcernsof
“what”vs. “how.” Theactualimplementationofanobjectis independentofitsuse.Theimplementation
canchange,butaslongastheinterfaceis preserved,othercomponentsthatrelyontheobjectwillnotbreak.
Encapsulationallowsustoisolatemajordesigndecisions,especiallyonesthataresubjecttochange.
Anotheradvantageofencapsulationisthatit supportscodereuse. It allowsustopackageupgeneral
componentsthatcanbeusedfromoneprogramtothenext.TheDieViewclassandButtonclassesare
goodexamplesofreuseablecomponents.
Encapsulationis probablythechiefbenefitofusingobjects,butalone,it onlymakesa systemobject-
based. To betrulyobjected-oriented, theapproachmustalsohave thecharacteristicsofpolymorphismand
inheritance.
12.4.2 Polymorphism
Literally, thewordpolymorphismmeans“many forms.” Whenusedinobject-orientedliterature,thisrefers
tothefactthatwhatanobjectdoesinresponsetoa message(amethodcall)dependsonthetypeorclassof
theobject.
Ourpokerprogramillustratedoneaspectofpolymorphism.ThePokerAppclasswasusedbothwith
aTextInterfaceandaGraphicsInterface. Thereweretwo differentformsofinterface,andthe
PokerAppclasscouldfunctionquitewellwitheither. WhenthePokerAppcalledtheshowDicemethod,
forexample,theTextInterfaceshowedthediceonewayandtheGraphicsInterfacedidit an-
other.
Inourpokerexample,weusedeitherthetextinterfaceorthegraphicsinterface.Theremarkablething
aboutpolymorphism,however, is thata givenlineina programmayinvoke a completelydifferentmethod
fromonemomenttothenext.Asa simpleexample,supposeyouhada listofgraphicsobjectstodraw onthe
screen.Thelistmightcontaina mixtureofCircle,Rectangle,Polygon, etc.Youcoulddraw allthe
itemsina listwiththissimplecode:
for obj in objects:
obj.draw(win)
Nowaskyourself,whatoperationdoesthisloopactuallyexecute?Whenobjisa circle,it executesthe
drawmethodfromthecircleclass.Whenobjis a rectangle,it is thedrawmethodfromtherectangleclass,
etc.
Polymorphismgivesobject-orientedsystemstheflexibilityforeachobjecttoperformanactionjustthe
waythatit shouldbeperformedforthatobject.Beforeobjectorientation,thiskindofflexibilitywasmuch
hardertoachieve.
12.4.3 Inheritance
Thethirdimportantpropertyforobject-orientedapproaches,inheritance, is onethatwehave notyetused.
Theideabehindinheritanceis thata newclasscanbedefinedtoborrow behaviorfromanotherclass.The
new class(theonedoingtheborrowing)is calledasubclass, andtheexistingclass(theonebeingborrowed
from)is itssuperclass.
Forexample,if wearebuildinga systemtokeeptrackofemployees,wemighthave a generalclass
Employeethatcontainsthegeneralinformationthatis commontoallemployees.Oneexampleattribute
wouldbeahomeAddressmethodthatreturnsthehomeaddressofanemployee.Withintheclassofall
employees,wemightdistinguishbetweenSalariedEmployeeandHourlyEmployee. We couldmake
thesesubclassesofEmployee, sothey wouldsharemethodslikehomeAddress. However, eachsubclass
wouldhave itsownmonthlyPayfunction,sincepayis computeddifferentlyforthesedifferentclassesof
employees.
Inheritanceprovidestwo benefits.Oneis thatwecanstructuretheclassesofa systemto avoidduplication
ofoperations.We don’t have towritea separatehomeAddressmethodfortheHourlyEmployeeand
SalariedEmployeeclasses.A closelyrelatedbenefitis thatnew classescanoftenbebasedonexisting
classes,promotingcodereuse.