Upcoming SlideShare
×

# Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

1,592 views

Published on

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,592
On SlideShare
0
From Embeds
0
Number of Embeds
171
Actions
Shares
0
40
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

1. 1. Object-Oriented Programming Language Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding Atit Patumvan Faculty of Management and Information Sciences Naresuan University
2. 2. 2 The Three Key Concepts • Polymorphism: objects from different class can deﬁne methods that share the same name • Dynamic Typing: the determination of the class that an object belongs to unit the program is executing. • Dynamic Binding: the determination of the actual method to invoke an object unit program execution timeAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
3. 3. 3 Polymorphism: Same Name, Different ClassProgram 9.1 main.m01: #import "Complex.h"02:03: @implementation Complex Complex04:05: @synthesize real, imaginary; real : double06: imaginary : double07: -(void) print08: { print() : void09:! NSLog (@" %g + %gi ", real, imaginary); setReal(double) : void andImaginary(double) : double20: } add(Complex) : Complex21:22: -(void) setReal: (double) a andImaginary: (double) b23: {24:! real = a;25:! imaginary = b;26: }27:28: -(Complex *) add: (Complex *) f29: {30:! Complex *result = [[Complex alloc] init];31:! result.real = real + f.real;32:! result.imaginary = imaginary + f.imaginary;33:! return result;34: }35: @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
4. 4. 4 Polymorphism: Same Name, Different Class Fraction Complex int : numerator real : double int : denominator imaginary : double print() : void print() : void setTo(int) over(int) : (void) setReal(double) : void andImaginary(double) : double convertToNum : (double) add(Complex) : Complex add(Fraction) : Fraction reduce() : voidProgram 9.1 Fraction.m :38: -(Fraction *) add: (Fraction * ) f38: {38:! Fraction * result = [[Fraction alloc] init];38:! result.numerator = numerator * f.denominator + denominator * f.numerator;38:! result.denominator = denominator * f.denominator;38:! [result reduce];38:38:! return result;38: } :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
5. 5. 5 Polymorphism: Same Name, Different Class Fraction Complex int : numerator real : double int : denominator imaginary : double print() : void print() : void setTo(int) over(int) : (void) setReal(double) andImaginary(double) : double convertToNum : (double) add(Complex) : Complex add(Fraction) : Fraction reduce() : voidProgram 9.1 Complex.m :38: -(Complex *) add: (Complex *) f38: {38:! Complex *result = [[Complex alloc] init];38:! result.real = real + f.real;38:! result.imaginary = imaginary + f.imaginary;38:! return result;38: } :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
6. 6. 6 Polymorphism: Same Name, Different ClassProgram 9.1 main.m :09: Fraction *f1 = [[Fraction alloc] init]; Complex10: !! Fraction *f2 = [[Fraction alloc] init];11: !! Fraction *fracResult; real : double12: !! Complex *c1 = [[Complex alloc] init]; imaginary : double13: !! Complex *c2 = [[Complex alloc] init];14: !! Complex *compResult; print() : void15: ! setReal(double) : andImaginary(double) : double16: !! [f1 setTo: 1 over: 10]; add(Complex) : Complex17: !! [f2 setTo: 2 over: 15];18: !19: !! [c1 setReal: 18.0 andImaginary: 2.5]; Fraction20: !! [c2 setReal: -5.0 andImaginary: 3.2]; : ! int : numerator25: !! compResult = [c1 add: c2]; int : denominator26: !! [compResult print]; : ! print() : void36: !! fracResult = [f1 add: f2]; setTo(int) over(int) : (void)37: !! [fracResult print]; convertToNum : (double) : !! add(Fraction) : Fraction reduce() : voidAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
7. 7. 7 Dynamic Binding and the id TypeProgram 9.2 main.m :09:! id dataValue;10:! Fraction *f1 = [[Fraction alloc] init];11:! Complex *c1 = [[Complex alloc] init];12:13:! [f1 setTo: 2 over: 5]; Fraction14:! [c1 setReal: 10.0 andImaginary: 2.5];15: int : numerator16:! // first dataValue gets a fraction int : denominator17:18:! dataValue = f1; print() : void19:! [dataValue print]; setTo(int) over(int) : (void)20: convertToNum : (double)21:! // now dataValue gets a complex number add(Fraction) : Fraction22:! dataValue = c1; reduce() : void22:! [dataValue print];23:!24:! [c1 release];25:! [f1 release]; dataValue f1 : numerator = 2 Fraction@yyyy denominator = 5 id Fraction@yyyyAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
8. 8. 8 Dynamic Binding and the id Type Fraction * f1 = [[Fraction alloc] init]; id f2 = f1; f1 Fraction@yyyy f2 Fraction numerator = 2 Fraction@yyyy denominator = 5 id Fraction@yyyyAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
9. 9. 9 Argument and Return Types with Dynamic Typing Fraction Complex int : numerator real : double int : denominator imaginary : double print() : void print() : void setTo(int) over(int) : (void) setReal(double) andImaginary(double) : double convertToNum : (double) add(Complex) : Complex add(Fraction) : Fraction reduce() : void id add (id dataValue1, id dataValue2) { return [dataValue1 add: dataValue2]; }Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
10. 10. 10 Methods for Working with Dynamic Types Method Question or Action -(BOOL) isKindOfClass: class-object Is the object a member of class-object or a descendant? -(BOOL) isMemberOfClass: class-object Is the object a member of class-object? Can the object respond to the method speciﬁed by -(BOOL) respondsToSelector:selector selector? +(BOOL) instancesRespondToSelector: selector Can instances of the speciﬁed class respond to selector? +(BOOL)isSubclassOfClass: class-object Is the object a subclass of the speciﬁed class? -(id) performSelector: selector Apply the method speciﬁed by selector Apply the method speciﬁed by selector, passing the -(id) performSelector: selector withObject: object argument object. -(id) performSelector: selector withObject: object1 Apply the method speciﬁed by selector with the withObject: object2 arguments object1 and object2.Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
11. 11. 11 Exception Handling Using @tryProgram 9.4 main.m01: #import "Fraction.h"02:03: int main (int argc, char *argv [])04: {05: ! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];06:! Fraction *f = [[Fraction alloc] init];07:! [f noSuchMethod];08:! NSLog (@"Execution continues!");09:! [f release];10:! [pool drain];11:! return 0;12: } \$ ./main 2012-04-08 12:36:14.589 main[4336:707] -[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10be0c210 2012-04-08 12:36:14.590 main[4336:707] *** Terminating app due to uncaught exception NSInvalidArgumentException, reason: -[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10be0c210 *** First throw call stack: ( ! 0 CoreFoundation 0x00007fff91708fc6 __exceptionPreprocess + 198 ! 1 libobjc.A.dylib 0x00007fff9344dd5e objc_exception_throw + 43 ! 2 CoreFoundation 0x00007fff917952ae -[NSObject doesNotRecognizeSelector:] + 190 ! 3 CoreFoundation 0x00007fff916f5e73 ___forwarding___ + 371 ! 4 CoreFoundation 0x00007fff916f5c88 _CF_forwarding_prep_0 + 232 ! 5 main 0x000000010bd37bf2 main + 194 ! 6 main 0x000000010bd376d4 start + 52 ) terminate called throwing an exceptionAbort trap: 6Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
12. 12. 12 @try and @ catch Statement @try { statement throw exception statement ... Exception } @catch (NSException *exception) { catch exception statement statement ... }Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
13. 13. 13 Exception Handling Using @tryProgram 9.5 main.m01: #import "Fraction.h"02:03: int main (int argc, char *argv [])04: {05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];06:! Fraction *f = [[Fraction alloc] init];07:08:! @try {09:! ! [f noSuchMethod];10:! }11:! @catch (NSException *exception) {12:! ! NSLog(@"Caught %@%@", [exception name], [exception reason]);13:! }14:! NSLog (@"Execution continues!");15:! [f release];16:! [pool drain];17:! return 0;18: }\$ ./main2012-04-08 12:45:32.562 main[4445:707] -[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10870c2102012-04-08 12:45:32.564 main[4445:707] Caught NSInvalidArgumentException-[Fraction noSuchMethod]: unrecognized selector sent toinstance 0x10870c2102012-04-08 12:45:32.564 main[4445:707] Execution continues!\$Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language