CMSC 132: Object-Oriented Programming IIObject-Oriented Design IIDepartment of Computer ScienceUniversity of Maryland, College ParkOverviewObject-oriented designObjects, methods⇒⇒⇒⇒Last lectureClasses, inheritance⇒⇒⇒⇒This lectureApplying object-oriented designElements of Object-Oriented DesignObjectsEntities in programMethodsFunctions associated with objectsClassesGroups of objects with similar propertiesInheritanceRelationship between classesClasses DefinitionGroup of objects with same state & behaviorAbstract description of a group of objectsSimilar to data typesType is a set of data values & their operationsExample ⇒⇒⇒⇒integer, real, boolean, stringCan view classes as types for objectsClasses PropertiesClasses provides classification for objectsEvery object belongs to some classObjects ⇒⇒⇒⇒instances (instantiations) of a classExample ClassGiven a class CarObjects can includeMyHonda, YourHondaHerMiniCooperHisSUVAll Car objectsShare same properties & behaviorMay have different values for propertiesCarInheritance DefinitionRelationship between classes when state and behavior of one class is a subset of another classTerminologySuperclass / parent ⇒⇒⇒⇒More general classSubclass ⇒⇒⇒⇒More specialized classSuperclassSubclassInheritancePropertiesSubclass inherits state & behavior of superclass“Is-a” relationship exists between inherited classesExample – train is a type of transportationInheritance Inheritance forms a hierarchyHelps organize classesInheritance is transitiveClass inherits state & behavior from all ancestorsInheritance promotes code reuseReuse state & behavior for classInheritance Hierarchy ExampleClassesThermostatAnalog thermostatDigital thermostatProgrammable thermostatForms of Inheritance SpecificationDefines behavior implemented only in subclassGuarantees subclasses implement same behaviorIn Java →abstract method in superclassSpecializationSubclass is customizedStill satisfies all requirements for parent classIn Java →override methodSpecialization ExampleForms of Inheritance ExtensionAdds new functionality to subclassIn Java →new methodLimitationRestricts behavior of subclassIn Java →override method, throw exceptionCombinationInherits features from multiple superclassesAlso called multiple inheritanceNot possible in JavaIn Java →implement interface insteadMultiple Inheritance Example CombinationAlarmClockRadio has two parent classesState & behavior from both Radio & AlarmClockSuperclassesApplying Object-Oriented Design 1.Look at objects participating in systemFind nouns in problem statement (requirements & specifications)Noun may represent class needed in design2.Look at interactions between objectsFind verbs in problem statementVerb may represent message between objects3.Design classes accordinglyDetermine relationship between classesFind state & methods needed for each class1) Finding ClassesThermostat uses dial setting to control a heaterto maintain constant temperature in roomNounsThermostatDial settingHeaterTemperatureRoomFinding ClassesAnalyze each nounDoes noun represent class needed in design?Noun may be outside systemNoun may describe state in classAnalyzing NounsThermostatCentral class in modelDial settingState in class (Thermostat)HeaterClass in modelRoomClass in modelTemperatureState in class (Room)HeaterThermostatDial SettingRoomTempFinding ClassesDecision not always clearPossible to make everything its own classApproach taken in SmalltalkOverly complex2+3 = 5 vs. NUM2.add(NUM3) = NUM5Impact of designMore classes ⇒⇒⇒⇒more abstraction, flexibilityFewer classes ⇒⇒⇒⇒less complexity, overheadChoice (somewhat) depends on personal preferenceAvoid making functions into classesExamples – class ListSorter, NameFinder2) Finding MessagesThermostat uses dial setting to control a heater to maintain constant temperature in room VerbsUsesControlMaintainFinding MessagesAnalyze each verbDoes verb represent interaction between objects? For each interactionAssign methods to classes to perform interactionAnalyzing VerbsUses“Thermostat uses dial setting…”⇒⇒⇒⇒Thermostat.SetDesiredTemp()Control“to control a heater…”⇒⇒⇒⇒Heater.TurnOn()⇒⇒⇒⇒Heater.TurnOff()Maintain“to maintain constant temperature in room”⇒⇒⇒⇒Room.GetTemperature()Example MessagesRoomThermostatHeaterGetTemperature()TurnOn() TurnOff()SetDesiredTemp()Resulting ClassesThermostatState – DialSettingMethods – SetDesiredTemp()HeaterState – HeaterOnMethods – TurnOn(), TurnOff()RoomState – TempMethods –
View Full Document