Extension In Objective C
If you have not read my last tutorial i suggest you please read from here: Learn Objective C
As You have seen in my last tutorial of category If you have not read Read from here: Category in Objective C.
I extend the Student Class With additional method of StudentSportsDisplay This Method is not need at all used in certain situations thats why i add it. While implementing Category be clear that method you are trying to add will added in class at run time.But not in case of Extension.
Extension extend the existing class or add the method to the source code at compile time.The method declared in class is implemented in Original Class Which you want to extend. It means Extension is used to hide the internal implementation detail by adding private method.Also People used it when they want to hide the private information of class method from other class. Extension is local to Class.
Now at last When Extension Word come think in mind that you want to extend the publice interface with additional private methods(can be property,instancevarible).If you write property it will synthesize automatically by compiler because getter and setter
method are implemented in Implementation file that is not in extension.
Why Extension used in Objective C?
1.) Extension through added method is compiled at Compile Time. So the Instance Variable can be added to it. But that is not added in case of Category.
2.)Extension hide the private information to accessible from other classes.
3.)Extension is local to class. Extension method declared not even avilable to inherited classes.
Now come through Practical. From above theory you might think that Extension has only header file not implementation file because its implementation done in original source code file of class privatelly.
Syntax to declare Extension is:
@interface ClassName()
@end
It will saved with ClassName_ExtensionName. In my case it is Student_StudentFeesDetail.
Example: In below example i have make private the instance variable fees by extending Student class. The whole program is executed only if private variable fees has assigned some value.
fees variable is private it can assigned Only in original class from which it is used.
First Select Extension file from menu as shown below:
First write Extension name then select class name for which is extended below is screenshoot:
In Student_StudentFeesDetail.h
#import "Student.h"
@interface Student ()
{
NSString *fees;
}
@end
In Student.h
@interface Student : NSObject
{
NSString *studentname;
}
@property(nonatomic,strong)NSString *StudentName;
@property(nonatomic,strong)NSString *Fees;
-(id)initWithStudentName:(NSString *)StudentName;
-(NSString *)setFeesDetail:(NSString *)Fees;
-(void)printDetail;
In Student.m
#import "Student_StudentFeesDetail.h"
@implementation Student
@synthesize StudentName=_StudentName;
@synthesize Fees=_Fees;
-(id)initWithStudentName:(NSString *)StudentName
{
studentname=StudentName;
return self;
}
-(NSString *)setFeesDetail:(NSString *)Fees
{
fees=Fees;
return fees;
}
-(void)printDetail
{
NSLog(@"The Student Name is: %@ and his fees for IOS Developement is:%@",studentname,[self setFeesDetail:@"$40000"]);
}
In main section
int main(int argc,const char * argv[])
{
NSAutorelease Pool=[[NSAutoRealese alloc]init];
Student *student1=[[Student alloc]initWithStudentName:@"abc"];
[student1 printDetail];
[pool drain];
return 0;
}
And the output will be as shown in it.

The Source Code of this tutorial downloaded from here: Extension in Objective C
I hope you like my tutorial. If you have any query about it please mail me or commented here.
My next tutorial will be Protocols In Objective C
If you have not read my last tutorial i suggest you please read from here: Learn Objective C
As You have seen in my last tutorial of category If you have not read Read from here: Category in Objective C.
I extend the Student Class With additional method of StudentSportsDisplay This Method is not need at all used in certain situations thats why i add it. While implementing Category be clear that method you are trying to add will added in class at run time.But not in case of Extension.
Extension extend the existing class or add the method to the source code at compile time.The method declared in class is implemented in Original Class Which you want to extend. It means Extension is used to hide the internal implementation detail by adding private method.Also People used it when they want to hide the private information of class method from other class. Extension is local to Class.
Now at last When Extension Word come think in mind that you want to extend the publice interface with additional private methods(can be property,instancevarible).If you write property it will synthesize automatically by compiler because getter and setter
method are implemented in Implementation file that is not in extension.
Why Extension used in Objective C?
1.) Extension through added method is compiled at Compile Time. So the Instance Variable can be added to it. But that is not added in case of Category.
2.)Extension hide the private information to accessible from other classes.
3.)Extension is local to class. Extension method declared not even avilable to inherited classes.
Now come through Practical. From above theory you might think that Extension has only header file not implementation file because its implementation done in original source code file of class privatelly.
Syntax to declare Extension is:
@interface ClassName()
@end
It will saved with ClassName_ExtensionName. In my case it is Student_StudentFeesDetail.
Example: In below example i have make private the instance variable fees by extending Student class. The whole program is executed only if private variable fees has assigned some value.
fees variable is private it can assigned Only in original class from which it is used.
First Select Extension file from menu as shown below:
First write Extension name then select class name for which is extended below is screenshoot:
#import "Student.h"
@interface Student ()
{
NSString *fees;
}
@end
In Student.h
@interface Student : NSObject
{
NSString *studentname;
}
@property(nonatomic,strong)NSString *StudentName;
@property(nonatomic,strong)NSString *Fees;
-(id)initWithStudentName:(NSString *)StudentName;
-(NSString *)setFeesDetail:(NSString *)Fees;
-(void)printDetail;
In Student.m
#import "Student_StudentFeesDetail.h"
@implementation Student
@synthesize StudentName=_StudentName;
@synthesize Fees=_Fees;
-(id)initWithStudentName:(NSString *)StudentName
{
studentname=StudentName;
return self;
}
-(NSString *)setFeesDetail:(NSString *)Fees
{
fees=Fees;
return fees;
}
-(void)printDetail
{
NSLog(@"The Student Name is: %@ and his fees for IOS Developement is:%@",studentname,[self setFeesDetail:@"$40000"]);
}
In main section
int main(int argc,const char * argv[])
{
NSAutorelease Pool=[[NSAutoRealese alloc]init];
Student *student1=[[Student alloc]initWithStudentName:@"abc"];
[student1 printDetail];
[pool drain];
return 0;
}
And the output will be as shown in it.

The Source Code of this tutorial downloaded from here: Extension in Objective C
I hope you like my tutorial. If you have any query about it please mail me or commented here.
My next tutorial will be Protocols In Objective C


No comments:
Post a Comment