Generalization is the process of factoring classes to define a hierarchy of shared elements organized into classes. Words such as "kind of" or "type of" are often used to describe the generalization association. The generalization association is unlike any other association, in fact association and generalization are treated as separate model elements. Associations draw relationships between separate objects. Generalization links classes together where each class contains a subset of the elements needed to define a single object. Instantiating all the element subsets from each of the classes in a single vertical column of the generalization results in a single object. Consequently multiplicity, roles, and constraints are not used with generalization.
To draw a generalization association, use a triangle at the superclass end of the generalization line and connect the other end of the generalization to the subclass. Add the discriminator on the generalization line in simple text.
The discriminator is a value or rule that distinguishes the subclasses from one another.Proper use of the discriminator facilitates the use of design patterns such as abstract factory.
Generalization Visibility
Generalization is the process of extracting shared characteristics from two or more classes,
and combining them into a generalized superclass. Shared characteristics can be
attributes,
associations,
or methods.
When specifying generalization, remember that attribute and operation visibility should be specified as protected.
Most languages will allow you set attributes and operations to private, but this will override the inheritance explicitly designed by the generalization association, thus introducing confusion into your model.
UML Generalization:
In UML modeling, a generalization relationship is a relationship in which one model element (the child) is based on another model element (the parent). Generalization relationships are used in class, component, deployment, and use case diagrams.
To comply with UML semantics, the model elements in a generalization relationship must be the same type.
For example, a generalization relationship can be used between actors or between use cases; however, it cannot be used between an actor and a use case. You can add generalization relationships to capture attributes, operations, and relationships in a parent model element and then reuse them in one or more child model elements.
Because the child model elements in generalizations inherit the attributes, operations, and relationships of the parent, you must only define for the child the attributes, operations, or relationships that are distinct from the parent.
The parent model element can have one or more children, and any child model element can have one or more parents.
It is more common to have a single parent model element and multiple child model elements.
Student Class Example using Specialization and Generalization:
The Student class is described as a specialization of the Person class. Conversely, the class Person is a generalization of the class Stud:ent. The specialized class Student is said to inherit all the features of its generalization class Person. Thus if any person has a name, then so do students. If we can ask a person for their age then we can do the same with a student. In fact, any operation that may be applied to a Person instance may also be applied to a Student instance. The converse, however, is not true. A Student object may have attributes and operations peculiar to it, such as a matriculation number. Since a Person is a generalization of a Student, only those common characteristics are applicable to Persons. Hence we are not permitted to ask a person for their matriculation number since this is only applicable to students. Specialization is the mechanism by which one class is defined as a special case of another class. The specialized class includes all the operations and attributes of the general class. The specialized class is said to inherit all the features (or characteristics) of the general class. The specialized class may introduce additional operations and attributes peculiar to it. In addition to the operations inherited, the specialized class may choose to redefine the behaviour of any one of these. This, as we shall see shortly, is used when the specialized class has a more specific way of defining that behaviour. The specialized class is commonly known as the subclass and the general class its superclass.
Analysis of the Class Diagram and Relationships
The given UML class diagram represents a hierarchical classification of Fruits, showing generalization and specialization relationships. Below is a detailed breakdown:
1. Class Hierarchy & Generalization
The topmost class is Fruit, which serves as a general (parent) class.
Fruit is specialized into two subclasses:
Apple
Watermelon
Each of these subclasses is further specialized into varieties:
Apple → GrannySmith, RedDelicious
Watermelon → Seedless, WithSeeds
This structure follows the generalization-specialization principle, where a parent class (Fruit) generalizes common characteristics, and child classes refine those characteristics.
2. Relationships in the DiagramGeneralization (Inheritance)
Represented by triangle-headed arrows pointing from subclass to superclass.
Meaning: The child classes inherit attributes and behavior from the parent class (Fruit).
Example:
Appleinherits from Fruit, meaning it retains all generic properties of fruits while adding its own specific characteristics.
Watermelon also inherits from Fruit.
Discriminator & Fruit Category
The discriminator (labeled "Fruit category") differentiates subcategories (Apple, Watermelon).
It helps classify different kinds of fruits under meaningful categories.
Aggregation or Variety Relationship
The next level of specialization includes:
Apple Varieties → GrannySmith, RedDelicious
Watermelon Varieties → Seedless, WithSeeds
Nature of Relationship:
These varieties are specialized subtypes of their respective parent classes.
They inherit properties and behaviors from their parent classes (Apple and Watermelon).
3. Summary of Class Structure
Class Level
Classes
Relationship
Parent Class (Superclass)
Fruit
General class for all fruits
Subclasses (Specialization)
Apple, Watermelon
Inherit from Fruit
Further Specialization (Varieties)
GrannySmith, RedDelicious, Seedless, WithSeeds
Inherit from Apple and Watermelon
4. Object-Oriented Design Principles Demonstrated
Inheritance (Generalization-Specialization) – Subclasses derive from a common parent.
Encapsulation – Each subclass contains specific attributes related to its type.
Abstraction – The Fruit class abstracts common behavior of all fruits.
Categorization – The discriminator (Fruit category) helps classify fruit types.
Conclusion: This UML class diagram effectively demonstrates a hierarchical classification of fruits using inheritance, with clear relationships between superclass, subclasses, and varieties.
Apple and Watermelon inherit from Fruit
GrannySmith and RedDelicious are subclasses of Apple
Generalization Hierarchy
Building a generalization hierarchy can work in two directions, from the most general class down to the most specialized classes (specialization), and from the most specialized classes to the most generalized class (generalization). Let us build from general down to specifics, using the class Dog. We need to keep track of various breeds in a dog school to understand and anticipate their unique training requirements.
To start the model begin with the dog class.
2) Identify the discriminator for the next level of differentiation or specialization, for example, Breed.
3) Connect all of the Breed level classes to the Dog class using the generalization type of association, the triangle at the superclass end of the association.
4) Place the discriminator on the generalization line.
5) To further specialize the hierarchy, select the discriminator for the next level. For example, break down Breed into sub-breeds, or sub-classes of the Spaniel class.
6) Connect the new sub-classes to the Spaniel class, again using the generalization symbol. Place the discriminator on the generalization line.
Composition Context Generalization- Quiz
Click the Quiz link below to take a short multiple-choice quiz on composition context and generalization. Composition Context Generalization - Quiz
In the next lesson, you will learn a valuable alternative to inheritance called delegation.