Sunday, 7 June 2015

Data Encapsulation In Objective C


Data Encapsulation In Objective C

In general Every Objective C program has two types of fundamental elements.
These are Action or code(Methods) and Data(Properties).


Program Action: These are those part of Objective C program which is responsible for performing action. In other words you can say Methods or message to object.

Program Data: These are the information about the objective c program used to pass the message the objects.

Now its time to describe what is Data Encapsulation?

Data Encapsulation is binding of data with action so that it can be safer from outside real world and nobody can interfer in other one’s implementation code.
In other words Data Encapsulation is the collection of action with data in dynamic manner.
Binding can be two types.

1.)Static Binding (At Compile Time)
2.) Dynamic Binding (At Run Time)

In Dynamic Binding binding of method and data at run time we will cover this tutorial lator on.

What is Interface?

Interface describes the Action and data what an object can perform with no detail about method and data. You have heard about Data Abstraction . Data Abstraction is the way to write only the interface by hiding the internal implementation details.

Let us look up simple example of data encapsulation I am not trying to confuse you by data encapsulation with other programs .

In Student.h class

@interface Student:NSObject
{
NSString *StudentName;
NSString *TeacherName;
}
-(id)initWithTeacher:(NSString *)TeacherName;
-(void)showClassTime:(NSInteger *)ClassTime;
-(void)showName;
@end


In Student.m class

@implementation Student

-(id)initWithTeacher:(NString *)TeacherName
{
NSString teacher,student;
Teacher=TeacherName;
NSLog(@”Student please enter your name:”);
Scanf(“%d”,StudentName);
student=StudentName;
Return self;
}

-(void)showName
{
NSLog(@”The %@ Teacher study the %@ student”Teacher,StudentName);
}
-(void)showClassTime:(NSInteger *)ClassTime
{
NSLog(@”Your Class will start from %@ Am”,ClassTime);
}
@end

In Main Class

int main (int argc,const char *argv[])
{
NSAutorealesePool *pool=[[NSAutorealesePool alloc]init];
Student *student1=[[Student alloc]initWithTeacher:@”Mr Suraj”];
[student1 showName];
[student1 showClassTime:@”9.00”];
[pool drain];
Return 0;
}

In above tutorial The instance variables StudentName,TeacherName are private to the class you can not use it outside from class.In other words it is hidden from external world but we want to use it in program and it can be with the help of encapsulating inside a method.

showName and showClassTime are the method used in interface to meet with external world by hiding the data that is StudentName and TeacherName.
I am using Instance Variables here but you need to use property in it.

I hope you like my tutorial.

My Next Tutorial will be init function,id,self and super.

3 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IOS development, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on IOS development . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete