articles

Home / DeveloperSection / Articles / Objective-C : Polymorphism

Objective-C : Polymorphism

Anonymous User3708 16-Jul-2015

Previously we learn how command line arguments works : Objective-C : Command-Line Arguments 

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

Polymorphism is the idea that the code which is executed when a message is sent to an object depends on both the receiver's class and the name of the method in the message. In traditional procedural languages, the code which is executed by a function call is determined by the name of the function only.

Objective C's support for polymorphism is simple: the type of the receiver of a message is not determined until run-time, so the method which is executed is not decided until run-time, and that decision is based on the receiver's class.

For example, suppose Window is a subclass of View, and they both implement a method called flush.


id  window = [[Window alloc] init];

id  view = [[View alloc] init];

id  anObject;

[view flush];      // the flush method in View

[window flush];    // the flush method in Window

anObject = view;

[anObject flush];  // the flush method in View

anObject = window;

[anObject flush];  // the flush method in Window


Since the compiler does not try to statically bind methods to messages, the run-time system must decide which code gets executed for a given message. The decision is based on the type of the receiver when the message is sent. The "universal object identity" type, id, allows this code to compile without warning; the compiler does not try to check that the type of the receiver matches the method being invoked (static typing).

It is a concept in object oriented programming languages, like C++ and Java, where objects can inherit functionality from a base class, and change it to provide specialised behaviour.

Consider the example, we have a class Shape that provides the basic interface for all the shapes. Square and Rectangle are derived from the base class Shape.


We have the method printArea that is going to show about the OOP feature polymorphism.


#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

 

@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

 

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;

}

When the above code is compiled and executed, it produces the following result:

2015-07-16 06:50:50.844 demo[3509] The area of square is 100.000000
2015-07-16 06:50:50.845 demo[3509] The area is 50.000000

In the above example based on the availability of the method calculateArea and printArea, either the method in the base class or the derived class executed.


Polymorphism handles the switching of methods between the base class and derived class based on the method implementation of the two classes.


Next, we will learn about : Objective-C : Memory management methods


Updated 12-Aug-2020
I am a content writter !

Leave Comment

Comments

Liked By