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 relatedby inheritance.
Polymorphism is the idea that the code which is executed when amessage is sent to an object depends on both the receiver's class and the nameof the method in the message. In traditional procedural languages, the codewhich is executed by a function call is determined by the name of the functiononly.
Objective C's support for polymorphism is simple: the type ofthe receiver of a message is not determined until run-time, so the method whichis executed is not decided until run-time, and that decision is based on thereceiver's class.
For example, suppose Window is a subclass of View, and they both implement a methodcalled 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 tomessages, the run-time system must decide which code gets executed for a givenmessage. The decision is based on the type of the receiver when the message issent. The "universal object identity" type, id, allows this code tocompile without warning; the compiler does not try to check that the type ofthe receiver matches the method being invoked (static typing).
It is a concept in object oriented programming languages, likeC++ and Java, where objects can inherit functionality from a base class, andchange 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 methodprintArea 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 codeis compiled and executed, it produces the following result:
2015-07-16 06:50:50.844 demo[3509] The area of square is 100.0000002015-07-16 06:50:50.845 demo[3509] The area is 50.000000
In the above examplebased on the availability of the method calculateAreaand printArea, either the method inthe base class or the derived class executed.
Polymorphism handlesthe switching of methods between the base class and derived class based on themethod implementation of the two classes.
Next, we will learn about : Objective-C : Memory management methods
Leave a Comment