MSc-IT Study Material
January 2011 Edition

Computer Science Department, University of Cape Town
| MIT Notes Home | Edition Home |

Inheritance

We indicated earlier that object-oriented modelling recognised some specialised types of associations. The first, and most important, of these is the generalisation-specialisation association. You will have encountered this previously in when studying use case modelling; it is now time to examine this relationship in more detail.

As a reminder, the symbol for a generalisation-specialisation relationship is shown below:

Figure 5.4. Generalisation-specialisation represented in the UML

Generalisation-specialisation represented in the UML

The arrow points towards the most general class. In this example, we are considering part of a system to be used by an estate agency for assisting in finding suitable premises for small businesses to purchase. The class Premises represents any premises. Its important attributes are the address, floor size, type of use permitted, amount of parking space, price expected, and so on, but these are not all shown in the diagram for reasons of lack of space (ellipses are used to indicate this). There are two important sub-classes of premises: leasehold premises, and freehold premises. These have all the same properties of Premises, and some additional ones. For example, leasehold premises will naturally have an address and a size, but they will also have a lease duration, a ground rent, and some others. Similarly, freehold premises will have properties that are not applicable to leasehold premises, or to premises in general.

It would be an error to show the attributes address, floor area, and so on, in LeaseholdPremises and FreeholdPremises. These classes will automatically have these properties, as they will inherit them from Premises. The same mechanism applies to operations: any operations of Premises will apply also to its sub-classes and should not be shown again in the diagram except if the sub-classes do different things in these operations.

There may also be sub-classes of LeaseholdPremises and FreeholdPremises. These will inherit the properties of all their super-classes. So classes can form a complex hierarchy. When a sub-class inherits properties from a class other than its own base class, this is called indirect inheritance.

Terminology

A variety of different terminology is used to denote different parts of a generalisation-specialisation relationship. Using the above figure as an example, we could say:

  • Premises is a generalisation of LeaseholdPremises and FreeholdPremises

  • Premises is the base class of LeaseholdPremises and FreeholdPremises

  • Premises is the super-class of LeaseholdPremises and FreeholdPremises

  • LeaseholdPremises and FreeholdPremises are sub-classes of Premises

  • LeaseholdPremises and FreeholdPremises are specialisations of Premises

Multiple inheritance

It is possible for a class to be a sub-class of more than one base class.

For example, Radio and Television are sub-classes of ElectronicDevice. Television and Car are sub-classes of TaxableProperty. Television inherits properties of ElectronicDevice and of TaxableProperty. Radios are not taxable, and therefore cannot be a sub-class of TaxableProperty. Cars are not electronic. This is a clear example of multiple inheritance. This example is quite artificial, to illustrate a case where it is difficult to avoid multiple inheritance. In many cases it will be possible to restructure a model to avoid it. Note that some programming languages (including Java) do not allow multiple inheritance, so many designers avoid it as well.