Mixin-Based Inheritance

Mixin-Based Inheritance

Mixin-based Inheritance Gilad Bracha∗ William Cook Department of Computer Science Hewlett-Packard Laboratories University of Utah 1501 Page Mill Road Salt Lake City, UT 84112 Palo Alto, CA 94303-0969 Abstract prefix methods cannot be replaced; instead, the prefix may use the command inner to invoke the extended The diverse inheritance mechanisms provided by method code supplied by the subpattern. Given that the Smalltalk, Beta, and CLOS are interpreted as differ- code in a prefix is executed in any of its extensions, Beta ent uses of a single underlying construct. Smalltalk and enforces a degree of behavioral consistency between a Beta differ primarily in the direction of class hierarchy pattern and its subpatterns. growth. These inheritance mechanisms are subsumed in The underlying mechanism of inheritance is the same a new inheritance model based on composition of mix- for Beta and Smalltalk [3]. The difference between them ins, or abstract subclasses. This form of inheritance lies in whether the extensions to an existing definition can also encode a CLOS multiple-inheritance hierarchy, have precedence over and may refer to previous defini- although changes to the encoded hierarchy that would tions (Smalltalk), or the inherited definition has prece- violate encapsulation are difficult. Practical application dence over and may refer to the extensions (Beta). This of mixin-based inheritance is illustrated in a sketch of model shows that Beta and Smalltalk have inverted in- an extension to Modula-3. heritance hierarchies: a Smalltalk subclass refers to its parent using super just as a Beta prefix refers to its subpatterns using inner. 1 Introduction In the Common Lisp Object System (CLOS) and its predecessor, Flavors [13], multiple parent classes may A variety of inheritance mechanisms have been devel- be merged during inheritance. A class's ancestor graph oped for object-oriented programming languages. These is linearized so that each ancestor occurs only once [7]. systems range from classical Smalltalk single inheri- With standard method combination for primary meth- tance [8], through the safer prefixing of Beta [12, 10], ods, the function call-next-method is used to invoke the to the complex and powerful multiple inheritance com- next method in the inheritance chain. binations of CLOS [6, 9]. These languages have similar CLOS supports mixins as a useful technique for build- object models, and also share the view that inheritance ing systems out of mixable attributes. A mixin is an is a mechanism for incremental programming. However, abstract subclass; i.e. a subclass definition that may they differ widely in the kind of incremental changes be applied to different superclasses to create a related supported. family of modified classes. For example, a mixin might In Smalltalk, subclasses can add additional methods be defined that adds a border to a window class; this or replace existing methods in the parent class. As a mixin could be applied to any kind of window to cre- result, there is no necessary relationship between the ate a bordered-window class. Semantically, mixins are behavior of instances of a class and the instances of its closely related to Beta prefixes. subclasses. The subclass methods can invoke any of the Linearization has been criticized for violating encap- original superclass methods via super. sulation, because it may change the parent-child re- In Beta, a subpattern (subclass) definition is viewed as lationships among classes in the inheritance hierarchy an extension of a previously defined prefix pattern. As [16, 17]. But the mixin technique in CLOS depends in Smalltalk, new methods may be defined. However, directly upon linearization and modification of parent- ∗Supported by grant CCR-8704778 from the National Science child relationships. Rather than avoid mixins because Foundation. they violate encapsulation, we argue that linearization Appeared in Proceedings of the ACM Conference on is an implementation technique for mixins that obscures Object-Oriented Programming: Systems, Languages, and their true nature as abstractions. Applications (OOPSLA) October 21- 25, 1990. By modest generalization of the inheritance models c 1990 ACM. Copied by permission. in Smalltalk and Beta, a form of inheritance based on 1 composition of mixins is derived. Mixin-based inheri- the Person display method using super display to display tance supports both the flexibility of Smalltalk and the the name, and then displays the degree. The net effect security of Beta. It also supports the direct encoding would be to print \A. Smith Ph.D.". It would also be of CLOS multiple inheritance hierarchies without du- possible to prefix the name, as in the case of titles like plication of subclass definitions. However, since the hi- \Dr.", by printing the title before calling super. erarchy is encoded as an explicit collection of linearized The subclass Graduate specifies only how Graduates inheritance chains rather than as a single inheritance differ from Persons [19]. This difference may be indi- graph, some changes to the hierarchy (especially if they cated explicitly as a delta, or set of changes. In this case might violate Snyder's notion of encapsulation) cannot the set of changes is simply the new display method. easily be made. The original definition is also just a display method. Section 2 discusses the single-inheritance languages When combined, the new display method replaces the Smalltalk and Beta and shows that they support very original. different uses of a single underlying construct. Sec- To formalize this process, objects are represented as tion 3 analyzes multiple inheritance and linearization records whose fields contain methods [1, 15, 18, 3]. The in CLOS, with special focus on support for mixins. expression fa1 7! v1; ··· ; an 7! vng represents a record Section 4 presents a generalized inheritance mechanism with fields a1; : : : ; an and associated values v1; : : : ; vn. that supports the style of inheritance in Beta, Smalltalk, The expression r:a represents selection of field a from and CLOS, with explicit support for mixins. In Sec- a record r. Record combination is a binary operator, tion 5 we sketch an extension to Modula-3 that illus- ⊕, that forms a new record with the fields from its two trates the use of generalized inheritance. Finally, Sec- arguments, where the value is from the left argument tion 6 summarizes our conclusions. in case the same field is present in both records. For example, fa 7! 3; b 7! `x'g ⊕ fa 7! true; c 7! 8g replaces the right value of a to produce fa 7! 3; b 7! `x'; c 7! 8g. 2 Single Inheritance Languages To interpret super, it is necessary for the delta, or modifications, to access the original method inherited 2.1 Smalltalk Inheritance from Person. This is achieved by supplying the parent Inheritance in Smalltalk is a mechanism for incremental class methods as a parameter to the delta. The resulting derivation of classes. Smalltalk inheritance was adapted inheritance mechanism is an asymmetric combination of from Simula [5, 14], and serves as the prototypical in- a parametric delta ∆ and a parent specification P : heritance mechanism. The primary subtlety in the pro- C = ∆(P ) ⊕ P: cess of inheritance is the interpretation of the special variables self and super. Self represents recursion, or This definition is a form of single inheritance: P refers self-reference, within the object instance being defined. to the inherited parent while ∆ is an explicit set of The interpretation of self has been addressed in previ- changes. The two occurrences of P do not indicate that ous work [3, 4, 15]; in this paper we focus on the in- it is instantiated twice, but that its information is used terpretation of super. Consider the following pair of in two contexts: for the interpretation of super and to Smalltalk classes. provide methods for the subclass. Suppressing the in- class Person terpretation of hidden instance variables, the example instance variables: name above has the following form. method: display P = fdisplay 7! name.displayg name display ∆(s) = fdisplay 7! s.display, degree.displayg ∆(P ) = fdisplay 7! name.display, degree.displayg class Graduate superclass: Person Although deltas were introduced to make specifica- instance variables: degree tion of the inheritance mechanism more clear, deltas method: display are not independent elements of a Smalltalk program; super display. degree display they cannot stand on their own and are always part of a subclass definition, which has an explicit parent class. The class Person defines a name field and a method for In Smalltalk a subclass of Person may completely re- displaying the name. The subclass Graduate extends the place the display method with, for example, a routine display method to include the person's academic degree. that displays the time of day. In Smalltalk inheritance, For example, a graduate with name \A. Smith" and the subclass is in control: there is no way to define degree \Ph.D." would respond to the display method Person so that it forces subclasses to invoke its display by invoking the Graduate display method, which invokes method as part of their display operation. 2 2.2 Beta Inheritance resents any inner definitions supplied by subpatterns. For an instance of Person, the inner part of P 0 is bound Inheritance in Beta is designed to provide security from to the record of null methods: P 0(;). replacement of a method by a completely different A subpattern specifies additional attributes which method. Inheritance is supported in Beta by prefixing of may also refer to any further inner behavior in later definitions. Beta employs a single definitional construct, subpatterns. If the attributes defined in the subpattern the pattern, to express types, classes and methods. As are specified by ∆0, then the result of prefixing by P 0 is this generality can be confusing, we use a simpler syn- the following composition: tax that distinguishes among the different roles1.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us