You can use UML inheritance in MDriven Designer to model a general concept once and create specialized classes that reuse its attributes and associations.
What UML inheritance means
Classes represent concepts in a UML class diagram. UML inheritance defines an is-a relationship between two classes:
- A superclass is the general class.
- A subclass is a specialized class.
- A subclass has all attributes and associations of its superclass, and can add attributes and associations that apply only to that specialization.
For example, an Apple is a Fruit. An Apple can therefore use the information defined for Fruit, while also having Apple-specific information.
| Class | Role | Example members |
|---|---|---|
| Fruit | Superclass; the common definition for all fruit | Name, GrowsInTheseCountries
|
| Apple | Subclass of Fruit | Inherits Name and GrowsInTheseCountries; may add Apple-specific members
|
| Orange | Subclass of Fruit | Inherits Name and GrowsInTheseCountries; may add Orange-specific members
|
Inheritance is also called generalization and specialization. Fruit is a generalization of Apple and Orange. Apple and Orange are specializations of Fruit.
Read the notation
In a UML class diagram, inheritance is shown with a generalization association: a line ending in a large hollow triangle that points to the superclass.
For the Fruit example, the generalization arrows point from Apple to Fruit and from Orange to Fruit. Follow an arrow toward the more general class. Follow it in the opposite direction to find a specialization.
When you add a generalization association in MDriven Designer, the class's Superclass value is updated in the Object Inspector.
Model inheritance in MDriven Designer
Use inheritance when several classes describe the same kind of thing and share meaningful information.
- Create the general class. For example, create
Fruit. - Add the attributes and associations that apply to every fruit to
Fruit. For example, create an association fromFruittoCountryand name the Fruit endGrowsInTheseCountries. - Create specialized classes such as
AppleandOrange. - Add a generalization association from each specialized class to
Fruit. - Select a subclass and check the Object Inspector. Its Superclass should be
Fruit. - Add members to a subclass only when they do not apply to every Fruit.
- Save the model and resolve reported errors before continuing.
Avoid duplicate members
Do not declare the same attribute in both a superclass and one of its subclasses. The subclass already inherits the superclass member.
For example, if CarOwner is a superclass and Person is a subclass, define Name:String on only one applicable level in the hierarchy. Defining Name on both classes produces a duplicate-member error. In the Bootcamp Chapter 9 example, Name:String belongs on CarOwner, so it is removed from Person.
Use an abstract superclass when appropriate
An abstract class defines common structure but is not intended to have direct instances. In MDriven Designer, select the class and set IsAbstract to True in the Property Inspector. An abstract class label is displayed in italics.
For example, CarOwner can be abstract when every owner is represented by a more specific class such as Person or CarDealer. Both subclasses then inherit the common owner information and associations.
Put information at the right level
Place each attribute and association at the most general level where it is true.
| If the information applies to... | Put it on... | Example |
|---|---|---|
| Every instance in the hierarchy | The superclass | Put GrowsInTheseCountries on Fruit, not separately on Apple and Orange.
|
| Only one specialization | That subclass | Put an Apple-only attribute on Apple.
|
| Several classes that are not all the same kind of thing | Do not force inheritance; model an association or another concept instead | Use an association when the relationship is between separate concepts rather than an is-a relationship. |
This placement prevents repeated definitions and gives all subclasses the same inherited meaning. It also makes later changes safer: changing a common Fruit member changes the definition used by Apple and Orange.
Work with a superclass type
Inheritance lets you handle different specialized objects through their common superclass type. In the Fruit example, a country's exported fruits can be treated as Fruit objects because Apple and Orange are both Fruit.
Country malaysia = new Country(this.ServiceProvider());
Apple apple = new Apple(this.ServiceProvider());
Orange orange = new Orange(this.ServiceProvider());
apple.GrowsInTheseCountries.Add(malaysia);
orange.GrowsInTheseCountries.Add(malaysia);
foreach (Fruit fruit in malaysia.ExportsTheseFruits)
{
if (fruit is Apple)
{
// Do Apple-specific operations
}
else if (fruit is Orange)
{
// Do Orange-specific operations
}
}
The loop uses Fruit because that is the common superclass. The is Apple and is Orange checks identify the specialized runtime type when an operation applies only to that subtype.
Inheritance and associations
Inherited associations are available to subclasses. If Fruit has GrowsInTheseCountries, both Apple and Orange use that association through inheritance. Do not create separate copies of the association on each subclass unless the relationships have different meanings.
When a relationship itself needs information, such as a date, status, or other data about the connection, consider an association class rather than adding unrelated information to either endpoint class.
Limits
A UML model can describe multiple inheritance, where a class has more than one superclass. MDriven is implemented using C#, and the model uses a single superclass: a class can have one superclass or no superclass, not several.
