Inheritance
In an inheritance relationship, a subclass inherits all attributes and behaviors from its superclass. The subclass may also define additional features beyond those of the parent. For example, Bus, Taxi, and Sedan are all types of Car. They share common properties like name and the ability to drive on roads.
Realization
A realization relationship occurs when a class implements an interface (or abstract class), fulfilling all method contracts declared by that interface. For instance, both Car and Ship realize the Vehicle interface, which defines mobility as an abstract capability—each provides its own concrete implementation.
Composition
Composition denotes a strong "whole-part" relationship where the part cannot exist independently of the whole. If the composite object is destroyed, so are its components. For example, a Person is composed of a Head and a Body; these parts have no meaningful existence outside the person.
Aggregation
Aggregation also represents a "whole-part" relationship, but the parts can exist independently of the whole. Consider a BusDriver who wears a Uniform and Cap. These items belong to the driver while in use but can be reassigned to another driver or replaced.
Association
Association is a general structural relationship indicating that objects of one class interact with objects of another. Both composition and aggregation are specialized forms of association. Associations can be:
- Bidirectional: Each class references the other.
- Unidirectional: One class knows about the other, but not vice versa.
- Self-association: A class is associated with itself.
- Multiplicity-constrained: Specifies how many instances participate.
Multiplicity notation includes:
1..1: Exactly one0..*: Zero or more1..*: One or more0..1: Optional (zero or one)m..n: At least m, at most n (where m ≤ n)
Dependency
Dependency is a weaker, transient relationship where one class uses another temporarily—typically as a method parameter, local variable, or return type. For example, a Car depends on Fuel; without fuel, it cannot operate, but fuel is not part of the car’s structure.
Relationship Strength Hierarchy
Among the six relationships, composition, aggregation, and association share similar code representations but differ in semantic strength. Ordered from strongest to weakest:
- Inheritance
- Realization
- Composition
- Aggregation
- Association
- Dependency
Visual Notation and Mnemonics
- Inheritance/Realization Arrow Direction: Always points from subclass to superclass or from implementing class to interface. The sbuclass knows about the parent, not the other way around.
- Line Style:
- Solid line with hollow triangle → Inheritance (is-a)
- Dashed line with hollow triangle → Realization (interface implementation)
- Association vs. Dependency:
- Solid line → Association: stable, long-term (e.g., a class holds another as a field)
- Dashed line → Dependency: temporary usage (e.g., method parameter)
- Aggregasion vs. Composition (Diamond Notation):
- Hollow diamond → Aggregation: "has-a", parts can live independently (weak, empty container)
- Filled diamond → Composition: "contains-a", parts share lifecycle with whole (strong, full container)
Note: UML class diagrams do not use filled arrowheads for relationships.