Self,Super Keyword in Objective C
In an Objective C Objects can send messages to own. You
think that how object can send message to own. When I say send message to
object then think that method call with corresponding objects.
Why SELF keyword is required?
When there is calling of message required inside from a method with same object then it can be done through self keyword which send message to own. You do not need to call again and again simple one call from main method and all other call done automatically from indside a method but the control will reach after execution of method at that place from the point where call is made.
In below example we want to set student name at run
time(called at console) Also we want to check how many friend that student
whose name is set. We call from inside the setStudentName method.
Example:
In Student.h class
@interface Student:NSObject
{
NSString studentname,friendname;
}
@property(nonatomic,strong)NSString *StudentName;
@property(nonatomic,strong)NSString *FriendName;
-(void)setStudentName;
-(void)studentFriend;
-(void)showInformation;
@end
In Student.m class
@implementation Student
@synthesize StudentName=_StudentName;
@synthesize FriendName=_FriendName;
-(void)setStudentName
{
NSLog(@”Enter Student your name:”);
Scanf(“%d”,StudentName);
Studentname=self.StudentName;
[self studentFriend];
}
-(void)studentFriend
{
NSLog(@”Enter your Friend Name:”);
Scanf(“%d”, FriendName);
friendname=self.FriendName;
}
-(void)showInformation
{
NSLog(@”%@ has Friend Name: %@ is”studentname,friendname);
NSLog(@”%@ has Friend Name: %@ is”studentname,friendname);
}
Main Class
Int main(int argc,const char * argv[])
{
NSAutorealesePool *pool=[[NSAutorealesePool alloc]init];
Student *student1=[[Student alloc]init];
[student1 setStudentName];
//We do not need to write here calling friendname method it
is automatically called from same object from which above method is called.
[student1 showInformation];
[pool drain];
Return 0;
}
Now you can see from above program the method friendStudent
is automatically called with same object inside a setStudentName method.
Super Keyword
Next we come up with super keyword. When a derived class is
inherited from base class
And call is made up with derived class object then you can also
use that object to call the base class method.
In Teacher.h class
@interface Teacher:NSObject
@property(nonatomic,strong)NSString *TeacherName;
@property(nonatomic,strong)NSString *TeacherName;
-(void)checkStudentTeacher:(NSString *)TeacherName;
@end;
In Teacher.m class
@synthesize TeacherName=_TeacherName;
-(void)checkStudentTeacher:(NSString *)TeacherName
{
NSLog(@"The %@ Student is studied by %@ Teacher",studentname,TeacherName);
}
-(void)showResult
{
[self checkStudentTeacher:@"Suraj"];
}
-(void)showResult
{
[self checkStudentTeacher:@"Suraj"];
}
In Student.h class
@interface
Student:Teacher
{
NSString *studentname;
}
@property(nonatomic,strong)NSString *StudentName;
{
NSString *studentname;
}
@property(nonatomic,strong)NSString *StudentName;
-(id)initWithTeacherName:(NSString *)StudentName;
-(void)showInformation;
@end
In Student.m class
@implementation Student
@syntheisize StudentName=_StudentName;
-(id)initWithStudentName:(NSString *)StudentName;
{
studentname=StudentName;
Return self;
}
-(void)showInformation
{
[super checkResult];
}
{
NSAutorealesePool *pool=[[NSAutorealesePool alloc]init];
Student *student1=[[Student alloc]initWithStudentName:@"abc”];
[student1 showInformation];
[pool drain];
Return 0;
}
In the above example Student class object is created.With
the help of this object first call is made up to method showInformation of Student class.Due to inheritance same pointer or object is call in base class with super keyword used to
Then Control is reached at the end same location in inherit class at end where call is made up.
I hope you like my tutorial .

base class can call the method of derived class? i'm not very understand,please help me.
ReplyDeleteZhigang Fang Welcome In blog. It was just a big mistake now i updated it. thanks for informing me after catching the error.
DeleteThe blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..
ReplyDeletePhonegap Application Development Company Bangalore | Phonegap Application Development Company Bangalore
Health Is God expects to convey the most ideal wellbeing surveys of the supplement accumulations and different wellbeing generation that range from skincare to mind, muscle, male upgrade and cerebrum wellbeing conditions. You, the client are of most extreme significance to us, and we are focused on being the entrance that maintains your sound way of life.
ReplyDeleteYou have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. Web Development Company in Bangalore | Website Designing Companies in Bangalore | Top Web Design Company in Bangalore
ReplyDelete