Start Diagramming!

<- Previous

5e. UML Elements - Generalization

Let's continue talking about relationships by learning about a special relationship for establishing inheritance. What we mean is the "Generalization" element.

What is a Generalization?

"Generalizations define generalization/specialization relationships between Classifiers. Each Generalization relates a specific Classifier to a more general Classifier. The immediate generalizations are also called the Classifier's parents, and where the Classifier is a Class, its superClasses...An instance of a Classifier is also an (indirect) instance of each of its generalizations."

A Generalization is a relationship between two Classes that signifies that one Class is a more general form than the other more specific form of the Class.

In our example, a "Screwdriver" would be considered a moe specific form of the more general "Tool" Class. In our other example, a "Customer" would be considered a more specific form of the general "Person" Class. The glue that connects each of those two Classes together is the "Generalization" element.

As mentioned in the specification, instances of the more specific Class can be considered indirectly as instances of the more general class. So, an instance of the "Screwdriver" Class is also an instance of the "Tool" Class.

A Generalization is shown in a Class Diagram as an arrow with an open arrowhead pointing from the direction of the specific Class toward the general Class.

Generalization arrow
Generalization arrow

Generalization "general" and "specific"

A Generalization is very simple. It only specifies a "general" Class and a "specific" Class.

Comparison to Object-Oriented Programming Languages

Generalization directly relates to inheritance in Object-oriented Programming Languages. This is largly implemented in other languages using the "extends" keyword, by the ":" operator in Class names, or by parenthesis "()" notation.

After implementing generalization using these keywords or operators, languages use "super" keywords to access more general class methods or properties.

One difference between UML and other Object-Oriented Langauges is that UML does not constrain inheritance to only one general Class. In other words, it support multiple inheritance. One Class can have multiple "generalizations" as well as multiple "specializations."

How do you know when to define a new Generalization?

This question is highly debated among software technologists, but in general the community has come to the conclusion to prefer "composition over inheritance." All this means is that when possible, you should avoid using Generalization relationships. For example, let's pretend we wanted to use our "Screwdriver" Class in a "Toolbox" app of some kind. Now let's say you started by Generalizing your tools into the "HandheldTool" general Class and "ElectricTool" general Class. Well, at first you decide that Screwdriver inherits from (or generalizes) the HandheldTool Class because the first few examples of Screwdriver instances look like they share the same properties as HandheldTools. But, then you come across an electric Screwdriver that you hold in your hand. "uh oh...which Class does Screwdriver generalize now?" This is one example of why composition is preferred over inheritance.

Having said that, there are times and places when Generalizations makes sense. A general rule could be that when you notice that multiple Classes share the same set of attributes or operations or feel like they are a part of a group of related Classes, it can be beneficial to implement inheritance by isolating those shared properties in a more general "base" Class and having each of the related Classes inherit from that general base Class.

Abstract Syntax

To illustrate what an Generalization is, UML provides abstract syntax which we have recreated in UML Class Diagrams.

Generalization Abstract Syntax
Generalization Abstract Syntax
Generalization Abstract Syntax Inherited Attributes
Generalization Abstract Syntax Inherited Attributes
Generalization Abstract Syntax Inherited Operations
Generalization Abstract Syntax Inherited Operations

Next ->