Generalization is the process of extracting shared characteristics from two or more classes,
and combining them into a generalized superclass. Shared characteristics can be
- 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.
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.
The Student class is described as a specialization of the Person class. Conversely, the class Person is a generalization of the class Student. 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.