Accessing Instance Variables and Data Encapsulation

You’ve seen how the methods that deal with fractions can access the two instance variables numerator and denominator directly by name. In fact, an instance method can always directly access its instance variables. A class method can’t, however, because it’s dealing only with the class itself, not with any instances of the class. (Think about that for a second.) But what if you wanted to access your instance variables from someplace else (for example, from inside your main routine)? You can’t do that directly because they are hidden. The fact that they are hidden from you is a key concept called data encapsulation. It enables someone writing class definitions to extend and modify the class definitions, ...

Get Programming in Objective-C, Sixth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.