Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee

Similar presentations


Presentation on theme: "Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee"— Presentation transcript:

1 Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee (lygu@gist.ac.kr)

2 polymorphism, dynamic typing, and dynamic binding. Polymorphism enables programs to be developed so that objects from different classes can define methods that share the same name. Dynamic typing defers the determination of the class that an object belongs to until the program is executing. Dynamic binding defers the determination of the actual method to invoke on an object until program execution time.

3 Polymorphism #import @interface ClassA: NSObject @property int class_a_property; -(void) action; @end @implementation ClassA @synthesize class_a_property; -(void) action { NSLog(@"ClassA action"); } @end @interface ClassB: NSObject -(void) action; @end @implementation ClassB -(void) action { NSLog(@"ClassB action"); } @end 2012-04-03 19:13:00.036 Chapt9[3695:707] ClassA action 2012-04-03 19:13:00.038 Chapt9[3695:707] ClassB action int main (int argc, const char * argv[]) { @autoreleasepool { ClassA* class_a=[[ClassA alloc]init]; ClassB* class_b=[[ClassB alloc]init]; [class_a action]; [class_b action]; } return 0; }

4 Dynamic typing& binding and the id type #import @interface ClassA: NSObject @property int class_a_property; -(void) action; @end @implementation ClassA @synthesize class_a_property; -(void) action { NSLog(@"ClassA action"); } @end @interface ClassB: NSObject -(void) action; @end @implementation ClassB -(void) action { NSLog(@"ClassB action"); } @end int main (int argc, const char * argv[]) { @autoreleasepool { ClassA* class_a=[[ClassA alloc]init]; ClassB* class_b=[[ClassB alloc]init]; id instance_id; instance_id = class_b; [instance_id action]; instance_id = class_a; [instance_id action]; } return 0; } 2012-04-03 19:17:37.940 Chapt9[3738:707] ClassB action 2012-04-03 19:17:37.942 Chapt9[3738:707] ClassA action

5 Compile time versus runtime checking #import @interface ClassA: NSObject @property int class_a_property; -(void) action; -(void) action_only_in_ClassA; @end @implementation ClassA @synthesize class_a_property; -(void) action { NSLog(@"ClassA action"); } -(void) action_only_in_ClassA { NSLog(@"action_only_in_ClassA"); } @end @interface ClassB: NSObject -(void) action; -(void) action_only_in_ClassB; @end @implementation ClassB -(void) action { NSLog(@"ClassB action"); } -(void) action_only_in_ClassB { NSLog(@"action_only_in_ClassB"); } @end int main (int argc, const char * argv[]) { @autoreleasepool { ClassA* class_a=[[ClassA alloc]init]; ClassB* class_b=[[ClassB alloc]init]; //Compile time error [class_a action_only_in_ClassB]; //Run time error id instance_id; instance_id = class_a; [instance_id action_only_in_ClassB]; } return 0; } 2012-04-03 20:54:25.917 Chapt9[4423:707] -[ClassA action_only_in_ClassB]: unrecognized selector sent to instance 0x100114100 2012-04-03 20:54:25.920 Chapt9[4423:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ClassA action_only_in_ClassB]: unrecognized selector sent to instance 0x100114100' *** First throw call stack: ( 0 CoreFoundation 0x00007fff84709fc6 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff85845d5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff847962ae -[NSObject doesNotRecognizeSelector:] + 190 3 CoreFoundation 0x00007fff846f6e73 ___forwarding___ + 371 4 CoreFoundation 0x00007fff846f6c88 _CF_forwarding_prep_0 + 232 5 Chapt9 0x0000000100001cc9 main + 137 6 Chapt9 0x0000000100001b24 start + 52 ) terminate called throwing an exceptionsharedlibrary apply-load-rules all Current language: auto; currently objective-c

6 The id Data Type and Static Typing If an id data type can be used to store any object, why don’t you just declare all your objects as type id? For several reasons, you don’t want to get into the habit of overusing this generic class data type. First, when you define a variable to be an object from a particular class, you are using what’s known as static typing. When you use static typing, the compiler ensures, to the best of its ability, that the variable is used consistently throughout the program. The compiler can check to ensure that a method applied to an object is defined or inherited by that class; if not, it issues a warning message.

7 Argument and Return Types with Dynamic Binding If you use dynamic binding to invoke a method, note the following rule: If a method with the same name is implemented in more than one of your classes, each method must agree on the type of each argument and the type of value it returns so that the compiler can generate the correct code for your message expressions. The compiler performs a consistency check among each class declaration it has seen If one or more methods conflict in either argument or return type, the compiler issues a warning message. If the inconsistency between two methods is just a different type of object (for example, the Fraction’s add: method takes a Fraction object as its argument and returns one, and the Complex’s add: method takes and returns a Complex object), the compiler will still generate the correct code because memory addresses (that is, pointers) are passed as references to objects anyway.

8 Selectors The method name part of a message is sometimes referred to as the selector or method selector because it is used by the runtime to select which of the receiver’s methods to execute. Selectors are just names, they don’t carry any type information. Objective-C defines a type, SEL, for holding a representation of a selector. A SEL has a one-to-one relationship with the selector name but a SEL is not itself a string. All selectors with the same name have the same SEL and different names always correspond to different SELs. Internally, Objective-C uses the SEL type to identify methods for reasons of efficiency. Using strings would be slow; just testing to see if two strings are the same requires looping over all the characters in the string.

9 The compiler directive @selector() converts a method name into a SEL: SEL aSelector = @selector( someMessageName ); If the method has any arguments, you must remember to include the colons when you create a SEL. They are part of the method name. @selector( aMethod ) and @selector( aMethod: ) are different SELs.

10 SELs can be used to make the message part of a message expression dynamic. NSObject defines the following method: - (id)performSelector:(SEL)aSelector When performSelector: is executed, the object executes the selector that is its argument. The following two lines of code are equivalent: [aShape draw]; [aShape performSelector: @selector( draw )];

11 Asking Questions About Classes

12 Exeption handling with @Try-@Catch #import @interface ClassA: NSObject @property int class_a_property; -(void) action; -(void) action_only_in_ClassA; @end @implementation ClassA @synthesize class_a_property; -(void) action { NSLog(@"ClassA action"); } -(void) action_only_in_ClassA { NSLog(@"action_only_in_ClassA"); } @end @interface ClassB: NSObject -(void) action; -(void) action_only_in_ClassB; @end @implementation ClassB -(void) action { NSLog(@"ClassB action"); } -(void) action_only_in_ClassB { NSLog(@"action_only_in_ClassB"); } @end int main (int argc, const char * argv[]) { @autoreleasepool { ClassA* class_a=[[ClassA alloc]init]; id instance_id; instance_id = class_a; @try { //Run time error [instance_id action_only_in_ClassB]; } @catch (NSException *exception) { NSLog(@"Caught %@%@", [exception name], [exception reason]); } return 0; } [Switching to process 4260 thread 0x0] [4260:707] -[ClassA action_only_in_ClassB]: unrecognized selector sent to instance 0x100114100 [4260:707] Caught NSInvalidArgumentException-[ClassA action_only_in_ClassB]: unrecognized selector sent to instance 0x100114100


Download ppt "Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee"

Similar presentations


Ads by Google