SlideShare a Scribd company logo
1 of 13
Download to read offline
Object-Oriented Programming Language
        Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding




                            Atit Patumvan
            Faculty of Management and Information Sciences
                          Naresuan University
2



                                                            The Three Key Concepts


             •        Polymorphism: objects from different class can define
                      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 time




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
3



                          Polymorphism: Same Name, Different Class

Program 9.1 main.m
01: #import "Complex.h"
02:
03: @implementation Complex
                                                                              Complex
04:
05: @synthesize real, imaginary;                         real : double
06:                                                      imaginary : double
07: -(void) print
08: {                                                    print() : void
09:! NSLog (@" %g + %gi ", real, imaginary);             setReal(double) : void andImaginary(double) : double
20: }                                                    add(Complex) : Complex
21:
22: -(void) setReal: (double) a andImaginary: (double) b
23: {
24:! real = a;
25:! imaginary = b;
26: }
27:
28: -(Complex *) add: (Complex *) f
29: {
30:! Complex *result = [[Complex alloc] init];
31:! result.real = real + f.real;
32:! result.imaginary = imaginary + f.imaginary;
33:! return result;
34: }
35: @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University          Object-Oriented Programming Language
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() : void
Program 9.1 Fraction.m
  :
38: -(Fraction *) add: (Fraction * ) f
38: {
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



                          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() : void



Program 9.1 Complex.m
  :
38: -(Complex *) add: (Complex *) f
38: {
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



                          Polymorphism: Same Name, Different Class

Program 9.1 main.m
  :
09:             Fraction *f1 = [[Fraction alloc] init];                                                    Complex
10:    !!       Fraction *f2 = [[Fraction alloc] init];
11:    !!       Fraction *fracResult;                                                real : double
12:    !!       Complex *c1 = [[Complex alloc] init];                                imaginary : double
13:    !!       Complex *c2 = [[Complex alloc] init];
14:    !!       Complex *compResult;                                                 print() : void
15:    !                                                                             setReal(double) : andImaginary(double) : double
16:    !!       [f1 setTo: 1 over: 10];                                              add(Complex) : Complex
17:    !!       [f2 setTo: 2 over: 15];
18:    !
19:    !!       [c1 setReal: 18.0 andImaginary: 2.5];                                                      Fraction
20:    !!       [c2 setReal: -5.0 andImaginary: 3.2];
  :    !
                                                                                     int : numerator
25:    !!       compResult = [c1 add: c2];
                                                                                     int : denominator
26:    !!       [compResult print];
  :    !                                                                             print() : void
36:    !!       fracResult = [f1 add: f2];                                           setTo(int) over(int) : (void)
37:    !!       [fracResult print];                                                  convertToNum : (double)
  :    !!                                                                            add(Fraction) : Fraction
                                                                                     reduce() : void




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                   Object-Oriented Programming Language
7



                                              Dynamic Binding and the id Type

Program 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];                                                                                      Fraction
14:!      [c1 setReal: 10.0 andImaginary: 2.5];
15:                                                                                             int : numerator
16:!      // first dataValue gets a fraction                                                    int : denominator
17:
18:!      dataValue = f1;                                                                       print() : void
19:!      [dataValue print];                                                                    setTo(int) over(int) : (void)
20:                                                                                             convertToNum : (double)
21:!      // now dataValue gets a complex number                                                add(Fraction) : Fraction
22:!      dataValue = c1;                                                                       reduce() : void
22:!      [dataValue print];
23:!
24:!      [c1 release];
25:!      [f1 release];                                            dataValue                                f1
  :
                                                                                                                                  numerator = 2
                                                                                     Fraction@yyyy
                                                                                                                                 denominator = 5
                                                                                               id                                      Fraction@yyyy

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                               Object-Oriented Programming Language
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@yyyy



Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                        Object-Oriented Programming Language
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



                              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 specified by
    -(BOOL) respondsToSelector:selector
                                                                                     selector?

    +(BOOL) instancesRespondToSelector: selector                                     Can instances of the specified class respond to selector?


    +(BOOL)isSubclassOfClass: class-object                                           Is the object a subclass of the specified class?


    -(id) performSelector: selector                                                  Apply the method specified by selector

                                                                                     Apply the method specified by selector, passing the
    -(id) performSelector: selector withObject: object
                                                                                     argument object.
    -(id) performSelector: selector withObject: object1                              Apply the method specified 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



                                                 Exception Handling Using @try

Program 9.4 main.m
01: #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: 6

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                      Object-Oriented Programming Language
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



                                                 Exception Handling Using @try

Program 9.5 main.m
01: #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: }

$ ./main
2012-04-08 12:45:32.562 main[4445:707] -[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10870c210
2012-04-08 12:45:32.564 main[4445:707] Caught NSInvalidArgumentException-[Fraction noSuchMethod]: unrecognized selector sent to
instance 0x10870c210
2012-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

More Related Content

What's hot

Applicative Logic Meta-Programming as the foundation for Template-based Progr...
Applicative Logic Meta-Programming as the foundation for Template-based Progr...Applicative Logic Meta-Programming as the foundation for Template-based Progr...
Applicative Logic Meta-Programming as the foundation for Template-based Progr...Coen De Roover
 
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...Make Mannan
 
A recommender system for generalizing and refining code templates
A recommender system for generalizing and refining code templatesA recommender system for generalizing and refining code templates
A recommender system for generalizing and refining code templatesCoen De Roover
 
Modeling Aspects with AP&P Components
Modeling Aspects with AP&P ComponentsModeling Aspects with AP&P Components
Modeling Aspects with AP&P Componentsmukhtarhudaya
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Ranel Padon
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaMichael Stal
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpMichael Stal
 
Lap trinh C co ban va nang cao
Lap trinh C co ban va nang caoLap trinh C co ban va nang cao
Lap trinh C co ban va nang caoVietJackTeam
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
OpenMI Developers Training
OpenMI Developers TrainingOpenMI Developers Training
OpenMI Developers TrainingJan Gregersen
 
OpenMI Developers Training
OpenMI Developers TrainingOpenMI Developers Training
OpenMI Developers TrainingJan Gregersen
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stalMichael Stal
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 

What's hot (20)

Unit 8
Unit 8Unit 8
Unit 8
 
Applicative Logic Meta-Programming as the foundation for Template-based Progr...
Applicative Logic Meta-Programming as the foundation for Template-based Progr...Applicative Logic Meta-Programming as the foundation for Template-based Progr...
Applicative Logic Meta-Programming as the foundation for Template-based Progr...
 
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
 
A recommender system for generalizing and refining code templates
A recommender system for generalizing and refining code templatesA recommender system for generalizing and refining code templates
A recommender system for generalizing and refining code templates
 
Modeling Aspects with AP&P Components
Modeling Aspects with AP&P ComponentsModeling Aspects with AP&P Components
Modeling Aspects with AP&P Components
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
Lap trinh C co ban va nang cao
Lap trinh C co ban va nang caoLap trinh C co ban va nang cao
Lap trinh C co ban va nang cao
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
OpenMI Developers Training
OpenMI Developers TrainingOpenMI Developers Training
OpenMI Developers Training
 
OpenMI Developers Training
OpenMI Developers TrainingOpenMI Developers Training
OpenMI Developers Training
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 

Similar to Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13alish sha
 
Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfadityastores21
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifestohu hans
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxvrickens
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guideTiago Faller
 
Programming basics
Programming basicsProgramming basics
Programming basicsillidari
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As CompDavid Halliday
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As CompDavid Halliday
 
Create a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfCreate a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfdevangmittal4
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cMayank Jalotra
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdflakshmijewellery
 
Lisandro dalcin-mpi4py
Lisandro dalcin-mpi4pyLisandro dalcin-mpi4py
Lisandro dalcin-mpi4pyA Jorge Garcia
 

Similar to Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding (20)

Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
 
Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdf
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifesto
 
Course1
Course1Course1
Course1
 
pyton Exam1 soln
 pyton Exam1 soln pyton Exam1 soln
pyton Exam1 soln
 
Deep C
Deep CDeep C
Deep C
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
 
Lab 2
Lab 2Lab 2
Lab 2
 
Aptitute question papers in c
Aptitute question papers in cAptitute question papers in c
Aptitute question papers in c
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
Create a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfCreate a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdf
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 
EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
 
14 Jo P Feb 08
14 Jo P Feb 0814 Jo P Feb 08
14 Jo P Feb 08
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdf
 
Lisandro dalcin-mpi4py
Lisandro dalcin-mpi4pyLisandro dalcin-mpi4py
Lisandro dalcin-mpi4py
 

More from Atit Patumvan

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agricultureAtit Patumvan
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)Atit Patumvan
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556Atit Patumvan
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationAtit Patumvan
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6Atit Patumvan
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsAtit Patumvan
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Atit Patumvan
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2Atit Patumvan
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1Atit Patumvan
 

More from Atit Patumvan (20)

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
 
Media literacy
Media literacyMedia literacy
Media literacy
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : Methods
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1
 

Recently uploaded

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Recently uploaded (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

  • 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 The Three Key Concepts • Polymorphism: objects from different class can define 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 time Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 3. 3 Polymorphism: Same Name, Different Class Program 9.1 main.m 01: #import "Complex.h" 02: 03: @implementation Complex Complex 04: 05: @synthesize real, imaginary; real : double 06: imaginary : double 07: -(void) print 08: { print() : void 09:! NSLog (@" %g + %gi ", real, imaginary); setReal(double) : void andImaginary(double) : double 20: } add(Complex) : Complex 21: 22: -(void) setReal: (double) a andImaginary: (double) b 23: { 24:! real = a; 25:! imaginary = b; 26: } 27: 28: -(Complex *) add: (Complex *) f 29: { 30:! Complex *result = [[Complex alloc] init]; 31:! result.real = real + f.real; 32:! result.imaginary = imaginary + f.imaginary; 33:! return result; 34: } 35: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 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() : void Program 9.1 Fraction.m : 38: -(Fraction *) add: (Fraction * ) f 38: { 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 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() : void Program 9.1 Complex.m : 38: -(Complex *) add: (Complex *) f 38: { 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 Polymorphism: Same Name, Different Class Program 9.1 main.m : 09: Fraction *f1 = [[Fraction alloc] init]; Complex 10: !! Fraction *f2 = [[Fraction alloc] init]; 11: !! Fraction *fracResult; real : double 12: !! Complex *c1 = [[Complex alloc] init]; imaginary : double 13: !! Complex *c2 = [[Complex alloc] init]; 14: !! Complex *compResult; print() : void 15: ! setReal(double) : andImaginary(double) : double 16: !! [f1 setTo: 1 over: 10]; add(Complex) : Complex 17: !! [f2 setTo: 2 over: 15]; 18: ! 19: !! [c1 setReal: 18.0 andImaginary: 2.5]; Fraction 20: !! [c2 setReal: -5.0 andImaginary: 3.2]; : ! int : numerator 25: !! compResult = [c1 add: c2]; int : denominator 26: !! [compResult print]; : ! print() : void 36: !! fracResult = [f1 add: f2]; setTo(int) over(int) : (void) 37: !! [fracResult print]; convertToNum : (double) : !! add(Fraction) : Fraction reduce() : void Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 7. 7 Dynamic Binding and the id Type Program 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]; Fraction 14:! [c1 setReal: 10.0 andImaginary: 2.5]; 15: int : numerator 16:! // first dataValue gets a fraction int : denominator 17: 18:! dataValue = f1; print() : void 19:! [dataValue print]; setTo(int) over(int) : (void) 20: convertToNum : (double) 21:! // now dataValue gets a complex number add(Fraction) : Fraction 22:! dataValue = c1; reduce() : void 22:! [dataValue print]; 23:! 24:! [c1 release]; 25:! [f1 release]; dataValue f1 : numerator = 2 Fraction@yyyy denominator = 5 id Fraction@yyyy Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 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@yyyy Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 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 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 specified by -(BOOL) respondsToSelector:selector selector? +(BOOL) instancesRespondToSelector: selector Can instances of the specified class respond to selector? +(BOOL)isSubclassOfClass: class-object Is the object a subclass of the specified class? -(id) performSelector: selector Apply the method specified by selector Apply the method specified by selector, passing the -(id) performSelector: selector withObject: object argument object. -(id) performSelector: selector withObject: object1 Apply the method specified 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 Exception Handling Using @try Program 9.4 main.m 01: #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: 6 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 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 Exception Handling Using @try Program 9.5 main.m 01: #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: } $ ./main 2012-04-08 12:45:32.562 main[4445:707] -[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10870c210 2012-04-08 12:45:32.564 main[4445:707] Caught NSInvalidArgumentException-[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10870c210 2012-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