Sami
Sami

Reputation: 139

How can i clear the concept of Encapsulating Data?

I was reading a tutorial about programing with objective C. I got the general idea about what is encapsulating data but the detail of it is still not clear. Can anyone help me?? I am learning it on my own so facing a bit of problems in making my concept clear. Thanks and Sorry in advance if its a silly question.

Upvotes: 3

Views: 63

Answers (1)

Marcelo Cantos
Marcelo Cantos

Reputation: 185862

Encapsulation simply means that access to the internal state of an object is only permitted through a defined interface. In the case of Objective-C, this includes methods and properties. You can read a property in one of two ways:

[foo prop]
foo.prop

They are pretty much the same thing, with the form being slightly more convenient, especially when chaining accessors (e.g., foo.bar.baz.prop vs [[[foo bar] baz] prop]). You also have two options when setting properties:

foo.prop = 1;
[foo setProp:1];

The only real difference I know of is that the dotted form (in both cases) requires knowledge of the type, whereas the method form doesn't, e.g.:

Foo * foo = …;
[foo setProp:1]; // OK
foo.prop = 1;    // OK

id bar = foo;
[bar setProp:1]; // OK
bar.prop = 1;    // Barf

Upvotes: 3

Related Questions