Home > DeveloperSection > Forums > I misunderstanding Encapsulation rules in objective c.
Tarun Kumar

Total Post:214

Posted on    August-20-2015 11:44 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 753  View(s)
Rate this:

I will explain my question from an example.

In .H file//

@interface Employee:NSObject {

@private NSString *name;


@property(nonatomic,retain) NSString *name;


in .M file//

@implementation {

@synthesize name; 



In this scenario when i access the name property within another class , like myEmp.Name = @"John"; it doesn't raise any issue. Does this according to the encapsulation rules or am I misunderstanding?

Jessica James
Jessica James

Total Post:16

Posted on    August-21-2015 8:50 AM

In Objective-C, only an instance method of object can access an instance variable. There is no way for an external object to access the instance variables of an object directly. The @private is only relevant to inheritance.


To make the variables accessable there are properties. A property defines a method, and methods on Objective-C are all public. There is no way in Objective-C do define private methods, you can only "hide" them by declaring them somewhere else than the public .h file (e.g. inside the .m file via @interface Employee() which declares an anonymous section).

Modified On Mar-18-2016 11:22:10 PM

Don't want to miss updates? Please click the below button!

Follow MindStick