Open In App

Polymorphism in Objective-C

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

One of the fundamental concepts in object-oriented programming is polymorphism. It enables objects to take on a variety of forms depending on their context. In Objective-C, polymorphism allows a class to have multiple behaviors depending on the type of data it is acting on. It is one of the key characteristics that distinguish Objective-C as a powerful and versatile programming language.

Types of Polymorphism in Objective-C

In Objective-C, there are two types of polymorphism:

  1.  Compile-time Polymorphism – Compilation time Polymorphism is also referred to as method overloading. The compiler determines which method to invoke in this type of polymorphism based on the number and type of arguments passed to it. Because the method to be invoked is determined at compile time, it is also known as static polymorphism.
  2.  Runtime Polymorphism – This is also referred to as method overriding. The method to be invoked in this type of polymorphism is determined at runtime based on the object that is calling the method. Because the method to be invoked is determined at runtime, it is also known as dynamic polymorphism.

Keyword Syntax and Related Terms

Both types of polymorphism have different syntaxes. We use the same method name but different parameters in compile-time polymorphism. We override a method in a subclass using the same method name and parameters in runtime polymorphism.

In Objective-C, we use the following keywords for polymorphism:

  1. @interface– defines the interface of a class
  2. @implementation– implements the methods of a class
  3. @selector– selects a method at runtime
  4. id– a pointer to an object of any type

Example of Compile-time Polymorphism

ObjectiveC




#import <Foundation/Foundation.h>
  
@interface Shape : NSObject
- (void)draw;
@end
  
@interface Circle : Shape
@end
  
@interface Square : Shape
@end
  
@implementation Shape
- (void)draw {
    NSLog(@"GeeksforGeeks drawing a shape");
}
@end
  
@implementation Circle
- (void)draw {
    NSLog(@"GeeksforGeeks drawing a circle");
}
@end
  
@implementation Square
- (void)draw {
    NSLog(@"GeeksforGeeks drawing a square");
}
@end
  
int main(int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Shape *shape;
    Circle *circle = [[Circle alloc] init];
    Square *square = [[Square alloc] init];
  
    shape = circle;
    [shape draw];
  
    shape = square;
    [shape draw];
  
    [pool drain];
    return 0;
}


In this code, we define a base class ‘Shape’, and two derived classes ‘Circle’ and ‘Square’. The ‘Shape’ class has a method ‘draw’ which is overridden by the ‘draw’ methods in the ‘Circle’ and ‘Square’ classes. In the main method, we create objects of type ‘Circle’ and ‘Square’ and assign them to a variable of type ‘Shape’. When we call the ‘draw’ method on these objects, the method of the corresponding subclass is called. 

Output:

The output of this program will be:

output

 

Example of Runtime Polymorphism

ObjectiveC




#import <Foundation/Foundation.h>
  
@interface Animal : NSObject
- (void)speak;
@end
  
@implementation Animal
- (void)speak {
    NSLog(@"Animal speaks");
}
@end
  
@interface Loin : Animal
- (void)speak;
@end
  
@implementation Loin
- (void)speak {
    NSLog(@"Loin Roars");
}
@end
  
int main() {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    Animal *animal = [[Animal alloc]init];
    Loin *loin = [[Loin alloc]init];
    Animal *loin2 = [[Loin alloc]init];
      
    [animal speak];
    [loin speak];
    [loin2 speak];
      
    [pool drain];
    return 0;
}


We define a base class ‘Animal’ and a derived class ‘Lion’ in this code. The ‘Animal’ class has a method called ‘speak,’ which is overridden by the ‘Lion’ class ‘speak’ method. In the main method, we create ‘Animal’ and ‘Lion’ objects. When we call the ‘speak’ method on these objects, we invoke the method of the corresponding subclass. We also create a ‘Lion’ object and assign it to an ‘Animal’ variable. Because of dynamic or runtime polymorphism, when we call the ‘speak’ method on this object, the ‘speak’ method of the ‘Lion’ class is still called. This program’s output will be:

output

 

Conclusion

In object-oriented programming, polymorphism is a powerful concept that allows for code reuse and flexibility. Through method overloading and method overriding, Objective-C supports both compile-time and runtime polymorphism. Developers can write more modular and maintainable code by utilizing these features.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads