D.1.6 Describe the relationships between objects for a given problem.
An inheritance relationship is the relationship that is identified between two classes used to create a new class. Note that inheritance involves only two classes with NO interaction between objects of those classes.
Generalization
Generalization is another name for inheritance or an "is a" relationship.
Generalization is a mechanism for combining similar classes of objects into a single, more general class. Generalization identifies commonalities among a set of entities. The commonality may be of attributes, behavior, or both. In other words, a superclass has the most general attributes, operations, and relationships that may be shared with subclasses. A subclass may have more specialized attributes and operations.
Specialization
Specialization is the reverse process of Generalization means creating new sub classes from an existing class.
Inheritance is an "is-a" relationship. also known as generalization.
Example
An "Employee" IS-A "Person".
Employee object does NOT need services of a Person object because . . . Employee object IS-A Person Object!
Example
A Bank Account is of two types - Savings Account and Credit Card Account. Savings Account and Credit Card Account inherit the common/ generalized properties like Account Number, Account Balance etc. from a Bank Account and also have their own specialized properties like unsettled payment etc.
Consider the differences and similarities between the classes of the following objects: pets, cats, tails, owners.
We see the following relationships:
owners feed pets, pets please owners (association)
a tail is a part of both dogs and cats (aggregation / composition)
a cat is a kind of pet (inheritance / generalization)
Figure 1: Three types of association connectors: association, aggregation, composition
If two classes in a model need to communicate with each other, there must be link between them, and that can be represented by an association (connector). Association can be represented by a line between these classes with an arrow indicating the navigation direction. In case arrow is on the both sides, association has bidirectional association.
We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association. The example indicates that a Student has one or more Instructors:
A single student can associate with multiple teachers:
The example indicates that every Instructor has one or more Students
We can also indicate the behavior of an object in an association (i.e., the role of an object) using role names.
The question "What is the difference between association, aggregation and composition" has been frequently asked. Actually, Aggregation and Composition are subsets of association meaning they are specific cases of association. In both aggregation and composition object of one class "owns" object of another class. But there is a subtle difference:
Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.
Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a House.
Aggregation allows code reuse / reduces amount of coding;
Aggregation allows for the better organization of objects;
As the variables/methods of the aggregated data will be kept in the class in which it belongs;
Aggregation increases dependencies;
A change in the in a dependent class could have an unexpected consequence for the other class;
Using aggregation can lead to more complex code;
When accessing functions of the aggregated object;
We should be more specific and use the composition link in cases where in addition to the part-of relationship between Class A and Class B - there's a strong lifecycle dependency between the two, meaning that when Class A is deleted then Class B is also deleted as a result
It's important to note that the aggregation link doesn't state in any way that Class A owns Class B nor that there's a parent-child relationship (when parent deleted all its child's are being deleted as a result) between the two. Actually, quite the opposite! The aggregation link is usually used to stress the point that Class A instance is not the exclusive container of Class B instance, as in fact the same Class B instance has another container/s.
Association is a very generic term used to represent when on class used the functionalities provided by another class. We say it's a composition if one parent class object owns another child class object and that child class object cannot meaningfully exist without the parent class object. If it can then it is called Aggregation.
Cardinality is the number of objects that participate in an association and whether the participation is optional or mandatory. Note the three questions at the bottom of page 161 that one must ask to determine cardinality. You should pay particular attention to the Employee class that inherits from the Person class on pages 161 – 163 which has composition relationships with Division, JobDescription, Spouse, and Child. Note the table for Cardinality of
Class Association in table figure 9.1.
Note that:
1. Employee objects are associated with from (0 to1) Spouse objects. (Optional)
2. Employee objects are associated with 1 and only 1 (1 to 1) Division objects. (Mandatory)
3. Employee objects are associated with from 1 . . . n (1 to many) JobDescription objects. (Mandatory)
4. Employee objects are associated with from 0 … n (0 to many) Child objects. (Optional)