简体   繁体   中英

Polymorphism in Objective-C - iOS

I have been using Polymorphism in C++ for a long time now and I rather like using it. Does Objective-C have this functionality? Maybe has something to do with Delegates?

I have been playing around with iOS development for a while and have been using Frameworks such as MessageUI and iAd.

So when I import these types of frameworks and then use their methods like this one for example:

- (void)mailComposeController:(MFMailComposeViewController*)controller  
      didFinishWithResult:(MFMailComposeResult)result 
                    error:(NSError*)error;

Does that mean I am essentially using Polymorphism in Objective-C?

By definition the word Polymorphism means many forms.

Polymorphism in general is very broad topic and basically it invokes lot of things like method overloading, operator overloading, inheritance, reusability.

And I don't think I implement polymorphism, rather I use the specific term like inheritance, operator overloading, method overloading etc

Objective-C polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

For example -

I have a base class Shape , defined as-

#import <Foundation/Foundation.h>

@interface Shape : NSObject {

    CGFloat area;
}

- (void)printArea;
- (void)calculateArea;
@end

@implementation Shape

- (void)printArea {
    NSLog(@"The area is %f", area);
}

- (void)calculateArea {
}

@end

I have derived two base classes Square and Rectangle from Shape as -

@interface Square : Shape { 

    CGFloat length;
}

- (id)initWithSide:(CGFloat)side;

- (void)calculateArea;

@end

@implementation Square

- (id)initWithSide:(CGFloat)side {
    length = side;
    return self;
}

- (void)calculateArea {
    area = length * length;
}

- (void)printArea {
    NSLog(@"The area of square is %f", area);
}

@end

@interface Rectangle : Shape {

    CGFloat length;
    CGFloat breadth;
}

- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth;

@end

@implementation Rectangle

- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth {
    length = rLength;
    breadth = rBreadth;
    return self;
}

- (void)calculateArea {
    area = length * breadth;
}

@end

Now the calling method on any object will invoke the method of the corresponding class as-

int main(int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Shape *square = [[Square alloc]initWithSide:10.0];
    [square calculateArea];
    [square printArea];
    Shape *rect = [[Rectangle alloc]
    initWithLength:10.0 andBreadth:5.0];
    [rect calculateArea];
    [rect printArea];        
    [pool drain];
    return 0;
}

And as you asked about

- (void)mailComposeController:(MFMailComposeViewController*)controller  
      didFinishWithResult:(MFMailComposeResult)result 
      error:(NSError*)error;

the above method is the delegate method of MFMailComposeViewController class and yes as it is implemented by the delegate implementer, so it can have the customized implementation as per the requirements within the legal guidelines and so it's also a form of polymorphism(as the delegate method could be implemented in more than one way).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM