Chapter 11 Implications of Inheritance The decision to supp ort inheritance and the principle of substitutabilit y in a language sets o� a series of c hain reactions that end up impacting almost ev ery asp ect of a programming language. In this c hapter, w e will illustrate this p oin t b y considering some of the implications of the decision to supp ort in a natural fashion the idea of the p olymorphic v ariable. The links b et w een inheritance and other language features can b e summarized as follo ws: � In order to mak e the most e�ectiv e use of ob ject-orien ted tec hniques, the language m ust supp ort the p olymorphic v ariable. A p olymorphic v ariable is a v ariable that is declared as one t yp e but actually main tains a v alue deriv ed from a subt yp e of the declared t yp e. � Because at compile time w e cannot determine the amoun t of memory that will b e required to hold the v alue assigned to a p olymorphic v ariable, all ob jects m ust reside on the heap, rather than on the stac k. � Because v alues are heap residen t, the most natural in terpretation of assignmen t and parameter passing uses reference seman tics, rather than cop y seman tics. � Similarly , the most natural in terpretation of equalit y testing is to test ob ject iden tit y . Ho w ev er, since often the programmer requires a di�eren t meaning for equalit y , t w o di�eren t op erators are necessary . � Because v alues reside on the heap, there m ust b e some memory managemen t mec ha- nism. Because assignmen t is b y reference seman tics, it is di�cult for the programmer to determine when a v alue is no longer b eing used. Therefore a garbage collection system is necessary to reco v er un used memory . Eac h of these p oin ts will b e more fully dev elop ed in the follo wing sections. 189
190 CHAPTER 11. IMPLICA TIONS OF INHERIT ANCE 11.1 The P olymorphic V ariable As w e will see in Chapter 12, a great deal of the p o w er of the ob ject-orien ted features of Ja v a comes through the use of a variable . A p olymorphic v ariable is declared as p olymorphic main taining a v alue of one t yp e, but in fact holds a v alue from another t yp e. W e ha v e seen man y suc h examples in the sample programs presen ted in P art 1. F or instance, m uc h of the standard user in terface co de thinks only that an application is an instance of class F rame , when in fact eac h program w e created used inheritance to create a new t yp e of application. Similarly , in the pin ball game construction program (Chapter 7) a v ariable w as declared as holding a v alue of t yp e PinBallT a rget , when in fact it w ould hold a Hole or a Sco reP ad . Figure 11.1 pro vides a class hierarc h y consisting of three classes, Shap e and t w o sub classes Circle and Squa re . In the small test program sho wn b elo w v ariable named fo rm is declared as t yp e Shap e , then assigned a v alue of t yp e Circle . As exp ected, when the function describ e() is in v ok ed, the metho d that is executed is the pro cedure in class Circle , not the function inherited from class Shap e . W e will use this example class in the subsequen t discussion in this c hapter. class ShapeTest f f static public void main (String [ ] args) Shape form = new Circle (10, 10, 5); System.out.prin tln (" for m is " + form.describe()); g g 11.2 Memory La y out Before w e can describ e the impact of the p olymorphic v ariable on memory managemen t, it is �rst necessary to review ho w v ariables are normally represen ted in memory in most programming languages. F rom the p oin t of view of the memory manager, there are t w o ma jor categories of memory v alues. These are stack b ase d memory lo cations, and he ap b ase d memory v alues. Stac k based memory lo cations are tied to pro cedure en try and exit. When a pro cedure is started, space is allo cated on a run-time stac k for lo cal v ariables. These v alues exist as long as the pro cedure is executing, and are erased, and the memory reco v ered, when the pro cedure exits. Figure 11.2 sho ws, for example, a snapshot of the run-time stac k for the follo wing simple recursiv e algorithm: class FacTest f static public void main (String [ ] args) f int f = factorial(3);
11.2. MEMOR Y LA YOUT 191 class Shape f protected int x; protected int y; public Shape (int ix, int iy) f x = ix; y = iy; g public String describe () f return "unknown shape"; g g class Square extends Shape f protected int side; public Square (int ix, int iy, int is) f g super(ix, iy); side = is; public String describe () f g return "square with side " + side; g f class Circle extends Shape protected int radius; public Circle (int ix, int iy, int ir) f super(ix, iy); radius = ir; g public String describe () f return "circle with radius " + radius; g g Figure 11.1: Shap e classes and P olymorphic V ariable
192 CHAPTER 11. IMPLICA TIONS OF INHERIT ANCE 0 n: 1 third 4 r: 1 activ ation 8 c: 0 record 0 n: 2 second 4 r: ? activ ation 8 c: 1 record 0 n: 3 �rst 4 r: ? activ ation 8 c: 2 record Figure 11.2: A Snapshot of the Activ ation F rame Stac k System.out.prin tln (" Fac to ria l of 3 is " + f); g f static public int factorial (int n) int c = n - 1; int r; if (c > 0) r = n � factorial(c); else r = 1; return r; g g The snapshot is tak en after the pro cedure has recursed three times, just as the innermost pro cedure is starting to return. The data v alues for three pro cedures are sho wn. In the innermost pro cedure the v ariable r has b een assigned the v alue 1, while the t w o p ending pro cedures the v alue of r has y et to b e determined. There are a n um b er of adv an tages of stac k based memory allo cation. All lo cal v ariables can b e allo cated or deallo cated as a blo c k, for example, instead of one b y one. This blo c k is commonly called an d . In ternally , v ariables can b e describ ed b y their activation r e c or n umeric o�set within the activ ation record, rather than b y their sym b olic address. These n umeric o�sets ha v e b een noted in Figure 11.2. Most mac hines are m uc h more e�cien t at dealing with n umeric o�sets than with sym b olic names. Notice that eac h new activ ation record creates a new set of o�sets, so that the o�set is alw a ys relativ e to the activ ation frame in whic h a v ariable app ears.
Recommend
More recommend