Open In App

Overriding Methods in Objective-C

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

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




// Objective-C program of method overloading
#import <Foundation/Foundation.h>
 
/* Creating MyClass interface prototype which
inherits the properties of NSObject */
@interface MyClass : NSObject  
  
- (NSString*)Name;
  
@end
 
/* Implementing the functions detail in MyClass
interface and this class will act as base class
in this program */
@implementation MyClass  
  
- (NSString*)Name
{
    return @"Default Name"
}
  
@end
 
/* Creating another class which will inherit the
properties of MyClass, MySubclass will derived
class and MyClass will be tthe base class */
@interface MySubclass : MyClass  
  
@end
 
/* implementation of subclass */
@implementation MySubclass 
  
- (NSString*)Name
{
    return @"Subclass Name";  
}
  
@end
 
int main (int argc, const char * argv[])  
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
         
        /* Creating object of MySubclass to invoke some functions */
        MySubclass *subclass = [[MySubclass alloc] init];  
         
        /* Here we are calling the function
        using MySubclass object */
        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




// Objective-C program of method overloading
#import <Foundation/Foundation.h> 
 
/* Creating MyClass interface prototype
which inherits the properties of NSObject */
@interface MyClass : NSObject   
  
- (NSString*)Name;
 
/* Declaring function prototype */
- (NSString*)Class;
  
@end
 
/* Implementing the functions detail in MyClass
interface and this class will act as base class
in this program */
@implementation MyClass
  
- (NSString*)Name
{
    return @"Default Name";  
}
- (NSString*)Class
{
    return @"class - 10"
}
  
@end
  
/* Creating another class which will inherit the
properties of MyClass, MySubclass will derived
class and MyClass will be the base class */
@interface MySubclass : MyClass  
 
 - (NSString*)Section;
  
@end
 
/* implementation of subclass */
@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];
         
         /* Creating object of MySubclass to invoke some functions */
        MySubclass *subclass = [[MySubclass alloc] init]; 
         
         /* Here we are calling the function Name
         using MySubclass object,
         This will execute the function of same class only */
        NSLog ([subclass Name]); 
         
         /* This statement will execute the function of its
         base class because there is no any function having
         same name in its same class */
        NSLog ([subclass Class]); 
         
        /* This statement will execute the function of its same class */
        NSLog ([subclass Section]);  
        [pool drain];
        return 0;
}


Output:

Subclass Name
class - 10
Section - A


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads