UML Class Diagram Relationships Explained: Inheritance, Composition, Aggregation, and More
Table of Contents
The hardest part of reading a class diagram is not the boxes — it is the lines. An arrow with a filled diamond means something completely different from one with an open diamond, or one with a hollow triangle. Get them wrong and your diagram tells a different story than your code.
This guide explains every UML class diagram relationship type, when to use each one, and how to write it in Mermaid syntax so you can draw it immediately.
Inheritance — The "Is-A" Relationship
What it means: One class is a specialized version of another. The subclass inherits attributes and methods from the parent class.
Example: Dog is-a Animal. Car is-a Vehicle. Administrator is-a User.
Arrow: A solid line with a hollow triangle pointing at the parent class.
Mermaid syntax:
Animal <|-- Dog
The hollow arrowhead (<|--) points toward the parent (Animal). Dog extends Animal.
When to use it: When a subclass genuinely replaces the parent in all contexts (Liskov Substitution Principle). If it does not pass that test, consider composition instead.
Composition — The Strong "Has-A" Relationship
What it means: One class owns another, and the child cannot exist without the parent. If the parent is destroyed, the child is destroyed too.
Example: A House has Rooms. If you demolish the House, the Rooms cease to exist. A Car has an Engine — the Engine is part of this specific Car.
Arrow: A solid line with a filled diamond on the owner's end.
Mermaid syntax:
House *-- Room
The filled diamond (*--) is on the House side. House owns Room.
When to use it: When the child object has no independent lifecycle — it is created and destroyed with the parent. This is the stronger of the two "has-a" relationships.
Aggregation — The Weak "Has-A" Relationship
What it means: One class contains another, but the child can exist independently of the parent. The relationship is a "uses" or "has" without full ownership.
Example: A Team has Players. If the Team is dissolved, the Players still exist — they just join another team. A Library has Books, but a Book can exist without a Library.
Arrow: A solid line with a hollow (open) diamond on the container's end.
Mermaid syntax:
Team o-- Player
The hollow diamond (o--) is on the Team side. Team aggregates Player.
When to use it: When the child object has its own independent lifecycle outside the parent. The distinction from composition is subtle — some teams use composition for both to keep diagrams simpler.
Sell Custom Apparel — We Handle Printing & Free ShippingAssociation — A General Relationship Between Classes
What it means: One class uses or knows about another, but there is no ownership. It is the most generic relationship — both classes are aware of each other and interact.
Example: A Customer places an Order. A Teacher teaches Students. A User submits a Report.
Arrow: A solid line with an open arrowhead, or sometimes just a plain line with no arrowhead (bidirectional).
Mermaid syntax:
Customer --> Order
The open arrowhead (-->) means Customer knows about Order (directed association). A plain line (--) is bidirectional — both classes know about each other.
When to use it: For relationships that do not fit inheritance, composition, or aggregation — where two classes interact without a clear parent-child or ownership structure.
Dependency and Realization
Dependency is the weakest relationship. One class uses another temporarily — it appears in a method signature, is created inside a method, or is passed as a parameter. The dependent class is not stored as an attribute.
Mermaid syntax:
OrderService ..> EmailService
The dashed arrow (..>) means OrderService depends on EmailService — it might call it in a method, but it does not own it.
Realization means a class implements an interface. It is like inheritance, but the parent is abstract (an interface or abstract class).
Flyable ..|> Bird
The dashed line with hollow triangle (..|>) means Bird realizes (implements) the Flyable interface.
Quick reference table:
| Relationship | Mermaid | Meaning |
|---|---|---|
| Inheritance | <|-- | is-a (subclass) |
| Composition | *-- | strong has-a (child dies with parent) |
| Aggregation | o-- | weak has-a (child lives on) |
| Association | --> | uses / knows |
| Dependency | ..> | temporary use |
| Realization | ..|> | implements interface |
Common Mistakes When Drawing UML Relationships
Using inheritance when you should use composition. Inheritance is tempting because it is the first relationship taught, but it creates tight coupling. If a Dog is-a Animal is the only reason you chose inheritance, ask: would composition work just as well? It usually does.
Confusing composition and aggregation. If you are unsure which to use, ask: "If I delete the parent, does the child make sense on its own?" Yes means aggregation. No means composition. In practice, many teams use composition for both to avoid the debate.
Drawing too many relationships. A class diagram with 30 arrows is unreadable. Focus on the structural relationships that matter for the decision at hand. You can always draw multiple diagrams with different scopes.
Pointing arrows in the wrong direction. In Mermaid, the hollow inheritance triangle points at the parent. So Animal <|-- Dog is correct (triangle at Animal). The filled composition diamond is on the owner side: House *-- Room (diamond at House).
Try It Free — No Signup Required
Runs 100% in your browser. No data is collected, stored, or sent anywhere.
Open Free Class Diagram ToolFrequently Asked Questions
What is the difference between composition and aggregation in UML?
Composition is a strong ownership relationship where the child object cannot exist without the parent. Aggregation is a weaker relationship where the child has its own independent lifecycle. In Mermaid syntax: *-- for composition, o-- for aggregation.
When should I use dependency instead of association?
Use dependency (dashed arrow) when one class only uses another temporarily — in a method call, as a parameter, or as a local variable. Use association when one class holds a reference to another as a persistent attribute.
What is realization in UML?
Realization means a class implements an interface or abstract class. It is shown as a dashed line with a hollow triangle, and in Mermaid uses ..|> syntax.
How do I draw these relationships in the free online class diagram tool?
Open the Badger Class Diagram tool and type your Mermaid classDiagram syntax. Use <|-- for inheritance, *-- for composition, o-- for aggregation, --> for association, ..> for dependency, and ..|> for realization. The diagram renders live as you type.

