Access Modifiers and Garbage Collection

Ads
 

Access Modifiers and Garbage Collection

Previously it was a requirement to allocate and release memory manually to assist with this problem it provides a reference-counting memory management system through retain and release keywords.

Previously it was a requirement to allocate and release memory manually to assist with this problem it provides a reference-counting memory management system through retain and release keywords.

Access Modifiers and Garbage Collection

     


Previously it was a requirement to allocate and release memory manually to assist with this problem it provides a reference-counting memory management system through retain and release keywords. But it is still required to take care of memory management by the programmer.
Going one step further in version 2.0 garbage collector is implemented as a conservative collector. This enable users to use full functionality of C as well as preserves Objective-C's ability to integrate with C++ code and libraries

Access Privileges

1.
Default access in objective-C is @protected.
2. Like C++ objective-C provide public and private access modifiers as well.
3.
@protected accessifier enable access elements in the subclass.

Example:
MyClass.h

#import<Foundation/NSObject.h>
@interface MyClass:NSObject {
	@private
    int a;
    int b;
}
   -(void) set:(int) x andb:(int) y;
   -(void) sum;
   -(void)show;
@end

MyClass.m

#import<stdio.h>
#import"MyClass.h"
@implementation MyClass
  -(void) set:(int) x andb:(int) y {
    a=x;
    b=y;
}
-(void) sum {
printf("Sum is : %d \n",a+b);
}
-(void)show{
printf("value of a is : %d \n",a);
printf("value of b is : %d \n",b);
}
@end

MyClassMain.m

#import<stdio.h>
#import"MyClass.m"
int main(){
MyClass *class1 = [[MyClass alloc] init];
MyClass *class2 = [[MyClass alloc] init];
[class1 set: 10 andb :12];
[class1 show];
[class1 sum];
// This is invalid statement because variable a is private.
// class2->a = 10;
class2->b = 15;
[class2 show];
[class2 sum];
[class1 release];
[class1 release];
return ;
}

Output:

value of a is : 10 
value of b is : 12 
Sum is : 22 
value of a is : 0 
value of b is : 15 
Sum is : 15 

Ads

Ads