Inheritance in NewtonScript

Combining Proto and Parent Inheritance

Think of a comb when you wish to visualize how proto and parent inheritance work together. The spine of the comb is the parent chain, while the individual teeth of the comb are proto chains. FIGURE 4.6 shows the inheritance relationship of a simple Newton program's views, NTK templates and protos. FIGURE 4.7 shows the more specific parent and proto inheritance relationship between some of the items in the program. As you can see, it loosely resembles a comb.

FIGURE 4.6 : Inheritance in a simple Newton Application.


(See "NewtonScript, Newton Toolkit, and the Newton" on page 109 for a complete discussion of the relationship among views, templates and protos.)

FIGURE 4.7 : Inheritance chain and lookup order.


Given the lookup order shown in FIGURE 4.7, it is easier to understand the basic rules of assignment when inherited slots are involved:

Now let us look at a diagram of several frames that have _parent and _proto slots in them. One of the frames has a method, SillyMethod, that makes assignments to slots in all of the frames.

Let us walk through what happens when SillyMethod is executed. The message we will send is:

one:SillyMethod();

one is searched for SillyMethod. When this fails, lookup follows the proto chain to two and searches for SillyMethod there. When lookup finds it, it sets self to one; SillyMethod is executed; and its first assignment is made:

a := "apple";

a is first looked up as a local in SillyMethod. Then it is searched for in self. When a is found directly within one, it gets assigned its new value right there.

The next statement in SillyMethod:

b := "boat";

is executed and lookup begins for b. First, it is looked up as a local in SillyMethod. Using inheritance, self (one) is searched for b, and then lookup follows the proto chain to two. This frame contains a b, so lookup is finished. To make a successful assignment requires creating a new slot with the value of "boat" in one (the same level at which the lookup found the slot). Remember, an assignment doesn't modify the proto.

SillyMethod now executes the third assignment:

c := "cat";

Lookup begins for c. SillyMethod is searched first for a local c. Using inheritance, self (one) is searched for c, and then lookup follows the proto chain to two. Now lookup, having exhausted the proto chain, starts up the parent chain of self in the quest for c. The parent of self (one) is three. This frame contains a c, so lookup is successful. The c slot in three is now assigned the new string value (as this is the level at which the lookup found the slot).

SillyMethod now executes the fourth assignment:

d := "dog";

Lookup begins for a local d in SillyMethod. Using inheritance, self is again searched for d, and then lookup follows the proto chain to two. Not finding the slot in the proto chain, lookup starts up the parent chain of self in its quest for d. three does not contain d, so lookup continues out the proto chain to four. This frame contains a d slot, so lookup is successful. Assignment to d requires creating a new slot with the value of "dog" in three. Assignment only changes slots along the spine of the comb, and assignment occurs at the level at which a slot is found.

The lookup occurs in the same way for the last three assignments to e, f, and g, though one more level up the parent chain. At this point, you will have new and modified slots on the comb's spine.

Accessing Slots
Testing for the Existence of Slots
Inherited
Using self in a Method
Don't Use _parent
Use Parent Inheritance for Inheriting Data

An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996