Accessing Instance Variables and Data Encapsulation
You’ve seen how the methods that deal with fractions can access the two instance variables
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, ...