Polymorphism in Objective-C
Last Updated :
24 Apr, 2025
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:
- 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.
- 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:
- @interface– defines the interface of a class
- @implementation– implements the methods of a class
- @selector– selects a method at runtime
- 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:
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:
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.
Similar Reads
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
Pointers in Objective-C
In Objective-C, Pointers are the variables that hold the memory address of another variable. You must have declared the pointer variable before its use. The size of the pointer depends on the architecture of the system. The pointer variable can be defined as a char, int, float, double, or any other
5 min read
Posing in Objective-C
In Objective-C, posing is a technique that allows a class to wholly replace another class within a program. The replacing class is said to "pose as" the target class. All messages sent to the target class are instead received by the posing class. This article focuses on discussing posing. What is Po
5 min read
Operators in Objective-C
The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using vari
11 min read
Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 min read
Preprocessors in Objective-C
Preprocessors help the software experts in tasks like Reusability and Conditional compilation. This article focuses on discussing Preprocessors in Objective-C. What is Objective-C?The Objective-C language is a general-purpose, dynamic, and high-level programming language designed to enable object-or
6 min read
Variables in Objective-C
A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
5 min read
Perl | Polymorphism in OOPs
Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as poly means many and morphism means types. Polymorphism is one of the most important concepts of object-oriented programming languages. The most common use of polymorphism in object
5 min read
Polymorphism in MATLAB
As with all other programming languages, MATLAB is also a programming language specially designed for Engineers. It was designed by Mathworks. Like all other programming languages, it also supports the concept of Object-oriented programming language (OOPs). The main concept is called Polymorphism. i
2 min read
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read