Objects with a Class Scope

Actually, this encapsulation usually takes place at the class level. The class is designed as a template of methods and variables--the common "mold" from which all instances for that class will be stamped. The instances themselves retain only the values of their variables.

Within the hierarchy, then, it is the class structure that ensures the integrity of a class's variables, so that only those methods designed to operate on them, do. (The class structure also provides for easy updating of the method code. If a method requires a change, you only have to change it once, at the class level. The change then is acquired by all the instances sharing the method.)

Associated methods and variables are said to have a certain scope, and in this case, that scope is the class to which they belong. In other words, the scope of a method and variable group defines or limits the range of objects that can share that group. Typically, a scope consists of the methods and object variables confined to a single class.
Scope of the Number class

┌─────────Scope of the Number class──────────┐
│ │
│ Instance methods Instance variables │
│ ---------------- ------------------ │
│ ADD number │
│ SUBTRACT │
│ MULTIPLY │
│ DIVIDE │
│ │
│ │
│ Number │
└─────────────── class ──────────────────┘

┌──┬──┬──┼──┬─────┐
│ │ │ │ │ │
1 2 3 4 5 ... n

instances

Each class in a class hierarchy has a separate scope from every other class. This is what allows a variable in a subclass to have the same name as a variable in a superclass, even though the methods that use the variables may be completely different. But scopes are not confined just to classes.


[Back: Determining the "Scope" of Methods and Variables]
[Next: Objects with Their Own Unique Scope]