Unformatted text preview:

1Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 118PolymorphismPoly => manyMorph => shapeVariables take on many shapes, or manyclasses of objects.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 119PolymorphismPolymorphism in OOP is caused by late-binding of procedure calls (messagelookup).Program can work with any object thathas the right set of methods.2Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 120Object Model for PayrollEmployeeEmployeeTransactionPayrollSystempost:datePaycheckTimecardSalaryChangesalarypay, taxeshoursWorkedvacationpostTo:abstract classabstract methodObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 121Examplespost: aTransactionaTransaction postTo: self.transactations add: aTransactionnumbers inject: 0 into: [:sum :i | sum + i]3Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 122The Case Against CaseStatementsSmalltalk has no case statement.OO programmers tend to avoid casestatements, and to use messagesending, instead.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 123Eliminating Casesexpcase: 1 do: [...];case: 15 do: [...].Make classes for 1 and 15, with a methodcalled msg.exp msg or (dictionary at: exp) msg4Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 124Class UndefinedObjectClass UndefinedObject has one instance --nil.All variables are initialized to nil.Also used to indicate illegal value.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 125Don’t Test ClassesUndefinedObject ObjectisNil isNil^ true ^ falsenotNil notNil^ false ^ trueDon’t test classes: use message sendingand inheritance.5Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 126Choicesbadx class = UndefinedObject ifTrue: [...]OKx = nil ifTrue: [...]bestx isNil ifTrue: [...]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 127Another choicex ifNil: [ … ]ObjectifNil: aBlock^selfUndefinedObjectifNil: aBlock^aBlock value6Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 128MagnitudeMagnitude ()ArithmeticValue ()Number () ....Point (’x’ ’y’)Character ()Date (’day’ ’year’)Time (’hours’ ’minutes’ ’seconds’)Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 129Number HierarchyNumberFraction (’numerator’ ’denominator’)Integer ()LargeNegativeInteger ()LargePositiveInteger ()SmallInteger ()...7Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 130Magnitude commentThe abstract class Magnitude providescommon protocol for objects that havethe ability to be compared along a lineardimension, such as dates or times.Subclasses of Magnitude include Date,ArithmeticValue, and Time, as well asCharacter and LookupKey.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 131Magnitude commentSubclasses must implement the followingmessages:comparing<=hash8Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 132NumbersNumbers are part of the class hierarchy, notbuilt into compiler.Numbers understand +, -, *, /, <, <=, etc.3 / 6 => 1 / 22 sqrt => 1.414213 + 4 * 2 =>Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 133Points, Numbers, Complex (not in image)squared"Answer the receiver multiplied by itself."^self * selfArithmeticObject9Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 134ArithmeticObject= aNumber"Answer whether the two objects are equal.For numeric objects, the two will be equal ifthe difference between them is zero"^aNumber respondsToArithmeticifTrue: [(self - aNumber) isZero]ifFalse: [false]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 135NumberNumber is also a magnitude.Adds truncation operationsFixedPoint, Fraction, Integer, Double,Float10Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 136Number// aNumber"Integer quotient defined by division withtruncation toward negativeinfinity. 9//4 = 2, -9//4 = -3. -0.9//0.4 =-3.\\ answers the remainder from thisdivision."^(self / aNumber) floorObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 137Number\\ aNumber"modulo. Remainder defined in termsof //. Answer a Number with the samesign as aNumber. e.g. 9\\4 = 1, -9\\4= 3, 9\\-4 = -3, 0.9\\0.4 = 0.1"^self - (self // aNumber * aNumber)11Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 138For an Abstract Class ...■ Look for "subclassResponsibility"methods to see the core methods.■ Look in subclasses to see examples.■ Flow of control goes up and down theclass hierarchy.• (So don't try to follow it!)■ Don't send "new" to the class.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 139Uses of InheritanceTo be shared by all subclasses• don't redefineDefault• probably redefine12Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 140Uses of InheritanceParameterized by subclasses (templatemethod)• rarely redefine• change it by redefining the methods itcallsTrue abstract methods• must redefineObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 141Uses of PolymorphismMethod lookup finds the method that isright for the receiver.• method always knows the class ofthe receiver• different classes lead to differentmethods13Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 142Uses of PolymorphismMethods often depend radically on classof receiver.isNilifTrue:ifFalse:double dispatchingObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 143Double-dispatching: the ProblemFloatSmallintegerFractionFixedPointMatrixFrScFiFl M*****FlFlFlFlFlFrFrFlFiFiScScSc ScScScScFiFi14Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 144ArithmeticNumber * MatrixMultiply each element by numberMatrix * NumberMultiply each element by numberMatrix * MatrixStandard matrix multiplicationObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 145ArithmeticInteger * Integer Primitive intoperationsInteger * Float Convert int to floatFloat * Integer Convert int to floatFloat * Float Primitive float operation15Object-oriented Programming and Design -


View Full Document

UIUC CS 497 - Polymorphism

Download Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?