UML Inheritance

With the ambition to make it easy for people to benefit from object-oriented approaches using MDriven and MDriven Designer, I will give a quick introduction to UML inheritance.

  • UML inheritance differs from “I get you stuff when you die.” It is also different from “Oh, look, that kid really looks like her Mother.”
  • UML inheritance is this: “A child class has all attributes and associations that a parent class has, and the child also has attributes and/or associations of its own that the parent does not have.” In other words, UML inheritance is “specialization” and “generalization”; a child class is a “specialized” version of the parent, and a parent is a more “generalized” definition of the child class.
  • UML inheritance is the same as OO inheritance (Object-oriented inheritance).
  • UML inheritance will allow you to inherit the properties of multiple parents – but very few OO languages allow for so-called multiple inheritances (c++ does, c# & VB.NET does not, and since ECO focuses on the latter languages, ECO does not support it either), so I will not mention multiple inheritances again in this post. This means a class can have only one parent class (or no parent class, but never many parents).

An Example

Fruit. Fruit is a pretty generic class. If we think of specializations of fruit, we will find Apple, Orange, Pear, Banana, Pineapple, etc.

FRUIT MODELS.png

The lines ending with the big arrow are called a Generalization-association, meaning that if you follow it, you get something more generalized of the class that you leave. If you follow it in the other direction, you get the opposite of generalization – specialization. You will notice that in MDriven when you add generalization associations, the class’s superclass is updated in the object inspector.

Properties fruits.png

Superclass is a more correct UML term than “Parent class.” Instead of “Child class,” the correct UML terminology is Subclass. So I will use Super- and Subclass from now on.

Why is Inheritance So Useful?

The obvious benefit of inheritance is the ability to introduce common properties that all fruit has in one place. If there are properties that all fruits have, they will go into the Fruit class rather than defining them over and over in the subclasses.

Model fruits + subclasses.png

The true power of inheritance is that it resembles how people reason and think. As humans, we always generalize. Our language and communication depend on it. This fact is the reason for some bad things in society – prejudice where we jump to conclusions based on earlier experience or hearsay – and some good things, like instantly knowing how to use a door knob even if we have never seen that particular type of door knob before.

With the model we now have, we can see the benefit that strong types give. Our code will now look like this:

1: Country malaysia=new Country(this.ServiceProvider());
2:
3: Apple apple = new Apple(this.ServiceProvider());
4: Orange orange = new Orange(this.ServiceProvider());
5:
6: apple.GrowsInTheseCountries.Add(malaysia);
7: orange.GrowsInTheseCountries.Add(malaysia);
8:
9: foreach (Fruit fruit in malaysia.ExportsTheseFruits)
10: {
11:     if (fruit is Apple)
12:     {
13:       // Do apple specific operations
14:     }
15:     else if (fruit is Orange)
16:     {
17:         // Do orange specific operations
18:     }
19: }

The MDriven Book - Next Chapter: Polymorphism

This page was edited 10 days ago on 10/02/2024. What links here