Generalization into superclasses and spezialization into subclasses is an important and valuable part of domain modelling. Inheritance from superclasses to subclasses reduces duplication in the model and makes sure common concepts are treated in the same manner.
An example of subclasses and superclasses is shown in the figure. Vehicle is a superclass for the subclasses Car and Train.
An other example, from ISO19157 - Data quality shows the abstract superclass DQ_Result with subclasses for different result types:
Spezialization into subclasses
The main motivation for spezializing a class into subclasses is if there are attributes, associations and/or operations that apply only to a identifiable selection of the class.
For example, the attributes on each subclass under DQ_Result apply only to a spesific kind for result. But there may also be situations where the motivation is just that the subclass is conceptualy different from other subclasses, even without special attributes, associations or operations. The subclass train is an example here. It has no special attributes or associations in the model, but it is something conceptualy different from a car. Such subclasses must be used with care, they may as well make the model more complex.
Subclasses are valuable for reducing duplication of attibutes, associations and operations, but must be used with care. Unnecessary subclasses makes the model more complex. Do not create subclasses without the motivations above.
Generalization into superclasses
The main motivation for generalization into superclasses is when several classes are variations of the same concept, with the same attributes, association and/or operations.
For example, in the figures above: Both Car and Train is a kind of vehicle, and may have a number of passengers. And all the subclasses under DQ_Result are kinds of results, with possible dates and scopes. By using the superclasses Vehicle and DQ_Result, the amount of duplication is reduced, and we make sure the same concept is used for all subclasses.
When the question comes up if whether or not classes can be generalized into a superclass, two simple rules shall be concerned:
- 100% rule: All of the superclass's required attributes and associations must be applicable also for the subclass
- Is a-rule: In a natural language - test the sentence subclass is a superclass (train is a vehicle)
In many cases, a superclass will be abstract, while the subclasses are the ones that are implemented.