next up previous contents
Next: Object-oriented rules Up: Object-oriented systems Previous: Classes   Contents


Inheritance

Class inheritance is directly supported by Maude's order-sorted type structure. Since class names are a particular case of sort names, a subclass declaration C < C' in an object-oriented module is just a particular case of a subsort declaration C < C'. The effect of a subclass declaration is that the attributes, messages, and rules of all the superclasses, together with the newly defined attributes, messages, and rules of the subclass, characterize the structure and behavior of the objects in the subclass.

For example, we can define an object-oriented module SAVING-ACCOUNT of saving accounts introducing a subclass SavingAccount of Account with a new attribute rate recording the interest rate of the account.

  class SavingAccount | rate : Float .
  subclass SavingAccount < Account .

In this example there is only one class immediately above SavingAccount, namely, Account. In general, however, a class $C$ may be defined as a subclass of several classes $D_{1},\dots,D_{k}$, i.e., multiple inheritance is supported. If an attribute and its sort have already been declared in a superclass, they should not be declared again in the subclass. Indeed, all such attributes are inherited. In the case of multiple inheritance, the only requirement that is made is that if an attribute occurs in two different superclasses, then the sort associated with it in each of those superclasses must be the same.15.1 In summary, a class inherits all the attributes, messages, and rules from all its superclasses. An object in the subclass behaves exactly as any object in any of the superclasses, but it may exhibit additional behavior due to the introduction of new attributes, messages, and rules in the subclass.

Objects in the class SavingAccount will have an attribute bal and can receive messages debiting, crediting and transferring funds exactly as any other object in the class Account. For example, the following object is a valid instance of class SavingAccount.

  < A-002 : SavingAccount | bal : 5000, rate : 3.0 >

As for subsort relationships, we can declare multiple subclass relationships in the same declaration. Thus, given classes A, ..., H, we can have a declaration of subclasses such as

  subclasses A B C < D E < F G H .


next up previous contents
Next: Object-oriented rules Up: Object-oriented systems Previous: Classes   Contents
The Maude Team