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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.