Overriding Methods in Objective-C
Last Updated :
20 Mar, 2023
If a subclass (child class) has the same method name as declared in the parent class, then it is known as method overriding in Objective C. Method overriding is a language feature in which a class can provide an implementation of a method that is already provided by one of its parent classes. The implementation in this class will replaces (that is, overrides) the implementation in the parent class. In other words, If a subclass provides some specific implementation of the method that has been declared by one of its parent class, then it is known as method overriding. Assume that we have two classes and one class inherits another class and both the classes have the same type of methods like the same name and same return type then in this situation method overriding occurs.
If we have a program consisting of two classes having the same type of method in each class and one class inherits another class then on method call in the main function if a function is present in the subclass then the subclass function will be executed because the first priority is given to the same class object.
Usage of Method Overriding
- Method overriding is used to provide some specific implementation of a method that is already provided by its superclass or parent class.
- Method overriding is used for runtime polymorphism.
@interface MyClass : NSObject
– (NSString*)Name;
@end
@implementation MyClass
– (NSString*)Name
{
return @”Default Name”;
}
@end
#import “MyClass.h”
@interface MySubclass : MyClass
@end
@implementation MySubclass
– (NSString*)Name
{
return @”Subclass Name”;
}
@end
If you create an instance of MyClass and send it a Name message, it returns ”Default value”. If you create an instance of MySubclass and send it a Name message, it returns ”Subclass value”. Notice that the subclass’s method must have the same name and parameter list as the superclass’s overridden method.
Example 1:
In this program we have two classes: MyClass and MySubclass and MySubclass is the base class of MyClass and both classes have the same type of method. Now in this situation Method overriding occurs, Now let us consider this example –
ObjectiveC
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
- ( NSString *)Name;
@end
@implementation MyClass
- ( NSString *)Name
{
return @"Default Name" ;
}
@end
@interface MySubclass : MyClass
@end
@implementation MySubclass
- ( NSString *)Name
{
return @"Subclass Name" ;
}
@end
int main ( int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init];
MySubclass *subclass = [[MySubclass alloc] init];
NSLog ([subclass Name]);
[pool drain];
return 0;
}
|
Output:
Subclass Name
Example 2:
In this program we have the same class/interface as in program 1 but for clear understanding, we have added one more method in each class like in MyClass we have added the Class method and in MySubclass we have added section method and now using object of MySubclass interface we can print all our methods, Now let us consider this example –
ObjectiveC
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
- ( NSString *)Name;
- ( NSString *)Class;
@end
@implementation MyClass
- ( NSString *)Name
{
return @"Default Name" ;
}
- ( NSString *)Class
{
return @"class - 10" ;
}
@end
@interface MySubclass : MyClass
- ( NSString *)Section;
@end
@implementation MySubclass
- ( NSString *)Name
{
return @"Subclass Name" ;
}
- ( NSString *)Section
{
return @"Section - A" ;
}
@end
int main ( int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init];
MySubclass *subclass = [[MySubclass alloc] init];
NSLog ([subclass Name]);
NSLog ([subclass Class]);
NSLog ([subclass Section]);
[pool drain];
return 0;
}
|
Output:
Subclass Name
class - 10
Section - A
Share your thoughts in the comments
Please Login to comment...