How objects relate to each other? - Part 7
This article explains association, aggregation, composition, dependency and inheritance, then applies each relationship to a grocery shop object model so beginners can choose connections deliberately.
How objects relate to each other? - Part 7
An object alone can do very little. A bill without items, a customer without bills or an item without a category is not useful for the business. The real value of an object-oriented design is in how the objects are connected, who knows whom, who owns whom and who just uses whom for a moment. When these relationships are not thought properly, everything is connected to everything, a change in the item breaks the customer screen and deleting a bill accidentally deletes the item from the catalogue. The real world is the same, a family, a company or a cricket team is defined as much by the relationships between its members as by the members themselves. In the previous article, we discussed subtype and subclass polymorphism. In this article, we are going to see the types of relationships between objects, association, aggregation, composition, dependency and inheritance, how to show them in a diagram and how to choose the right one.
Object-Oriented Thinking series:
- How to model the real world with objects? - Part 1
- How do we organize what we know? - Part 2
- What is abstraction? - Part 3
- What is encapsulation? - Part 4
- What is inheritance? - Part 5
- What are subtype and subclass polymorphism? - Part 6
- How objects relate to each other? (this article)
- How all these concepts connect? - Part 8
TL; DR Summary
- Association - Two objects know each other and work together, like a customer and a bill.
- Aggregation - A whole has parts, but the parts can live without the whole, like a shop and its items.
- Composition - A whole owns its parts, and the parts cannot live without the whole, like a bill and its lines.
- Dependency - One object uses another only for a short time to do a job, like a bill using a payment gateway.
- Inheritance - One concept is a special kind of another, like a weighed item is an item.
- Multiplicity tells how many objects take part in a relationship, like one customer places many bills.
- Fewer and weaker relationships make the design easier to change. Talk only to the objects you directly know.
Common Challenges
Everything knows everything
When we are not careful, the Item knows its bills, the Bill knows the Shop, the Shop knows all Customers, and the Customer knows all Items he ever bought. Each link was added for one small need. But now, to load one item, the program loads half of the database, and a change in any object can affect all others. This is called as tight coupling, and it is the main reason why some systems are very hard to change.
Wrong ownership
If the design doesn’t say who owns whom, nobody knows what should happen when an object is deleted. Deleting a bill must delete its lines, otherwise there are orphan lines in the system. But deleting a bill must not delete the item rice from the catalogue, otherwise the next customer cannot buy rice. Both are “the bill has items”, but the meaning is completely different.
Types of object relationships
The following diagram depicts the five common types of relationships between objects, with one example from our shop for each, drawn in the standard UML notation. They are arranged from the weakest on the left to the strongest on the right, the stronger the relationship, the more the two objects depend on each other.

Let us discuss each one with our shop.
Association
Association is a structural relationship where objects of one class are connected to objects of another class, they know each other and can send messages to each other, but neither owns the other.
In simple terms, association is “knows” or “works with”. A customer places bills, a bill line refers to an item, an item belongs to a category. Both sides have their own life, a customer exists before his first bill and after his last bill.
Every association has few properties,
- Name - What the relationship means, “places”, “refers to”.
- Multiplicity - How many objects on each side, we will see it in the next section.
- Direction (navigability) - Who knows whom. The bill line knows its item, but the item doesn’t need to know all the bill lines it is part of. One direction is simpler, use both directions only when it is really needed.
Multiplicity
Multiplicity tells how many objects of one class can be connected with one object of the other class. Here are the common values,
- 1 - Exactly one. Every bill belongs to exactly one customer.
- 0..1 - Zero or one. A customer may have a loyalty card.
- * or 0..* - Zero or many. A customer can have zero or many bills.
- 1..* - One or many. A bill must have at least one line.
Multiplicity is not just a detail in a diagram, it is a business rule. “A bill must have at least one line” means the system must not allow an empty bill to be saved. Always confirm the multiplicity with the business, it is a very common source of bugs when it is assumed.
Aggregation
Aggregation is a special kind of association which represents a whole and part relationship, where the parts can exist independently of the whole and can even be shared by more than one whole.
In simple terms, aggregation is “has a”, but loosely. Our shop has items, but the item rice exists in the supplier’s catalogue even if our shop is closed, and the same item can be sold by another branch. A cricket team and its players is another example, if the team is dissolved, the players still exist and can join another team.
Composition
Composition is a strong kind of aggregation, where the whole owns its parts exclusively, and the parts are created and destroyed with the whole.
In simple terms, composition is “is made of” or “owns”. A bill is made of bill lines. A bill line “2 x Rice = 100” has no meaning without its bill, it cannot be moved to another bill and when the bill is cancelled, its lines are gone. A house and its rooms is another example, you cannot sell a room separately and keep it when the house is demolished.
This is the “is a part of” hierarchy we discussed in Part 2, a room is a part of the house, not a kind of house. Keep in mind that composition is also the tool for encapsulation we discussed in Part 4. Since the bill owns its lines, the bill is the only one who creates and changes them, so it can protect the invariant “the total is equal to the sum of lines”.
Aggregation vs Composition
These two are always confused. Ask the following questions,
- If the whole is deleted, should the parts be deleted also? Yes means composition.
- Can a part belong to two wholes at the same time? Yes means aggregation.
- Can a part move from one whole to another? Yes, it is most likely aggregation.
Dependency
Dependency is the weakest relationship, where one object uses another object temporarily, for example as a method parameter or a local variable, to complete a job, without keeping a permanent link to it.
In simple terms, dependency is “uses for a moment”. When the bill is paid, it uses the payment gateway to process the card, then forgets it. The bill doesn’t store the gateway as part of its state. A person who takes a taxi depends on it for that trip only, he doesn’t own the taxi.
It is still a relationship, if the payment gateway changes how it is called, the bill may need to change. So, even the dependencies must be few and on stable things, preferably on an interface, as we discussed in the previous article.
Inheritance
Inheritance is the “is a” relationship between a general and a specific concept. It is the “is a kind of” hierarchy from Part 2, we discussed it in detail in Part 5 and its relationship with polymorphism in Part 6. Among all the relationships, it is the strongest one, since the child depends on the parent’s code and promises, even before any object is created.
How to show the relationships?
The UML (Unified Modeling Language) class diagram is the standard way to draw these relationships, so that any developer can read the design. Here is the list of common notations,
| Relationship | UML notation | Read as | Example |
|---|---|---|---|
| Association | Solid line, optional open arrow | knows / works with | Customer — Bill |
| Aggregation | Solid line, hollow diamond at the whole | has (shared parts) | Shop ◇— Item |
| Composition | Solid line, filled diamond at the whole | owns (part of) | Bill ◆— Bill line |
| Dependency | Dashed line, open arrow | uses temporarily | Bill - - > Payment gateway |
| Inheritance | Solid line, hollow triangle at the parent | is a | Weighed item —▷ Item |
| Realization | Dashed line, hollow triangle at the interface | implements | FestivalDiscount - -▷ DiscountRule |
The following diagram depicts the object model of our shop with all these relationships together. The customer places bills, the bill owns its lines, each line refers to an item, the shop sells the items, each item belongs to a category, a weighed item is an item and the bill uses the payment gateway to pay. The numbers near the lines are the multiplicity, for example, one customer places many bills and one bill is made of one or more lines.

How to choose the right relationship?
The relationships are not equal, some connect the objects more tightly than others. Find below the relationships from the weakest to the strongest,
| Strength | Relationship | Lifetime tied? | Coupling |
|---|---|---|---|
| Weakest | Dependency | No, only during a call | Very low |
| Association | No | Low | |
| Aggregation | No, parts are independent | Medium | |
| Composition | Yes, parts die with the whole | High (but local) | |
| Strongest | Inheritance | Fixed when the class is written | Very high |
Basically, use the weakest relationship which correctly describes the business. Here are few guidelines,
- Start with dependency or association, move to a stronger one only when the business needs it.
- Use composition when there is a clear ownership and lifetime, like bill and its lines, it gives the strongest protection for the invariants.
- Don’t use inheritance where association or composition is enough, as we discussed in Part 5.
- Keep the relationships one directional unless both sides really need to know each other.
- Talk only to your direct friends. The Law of Demeter, from a project at Northeastern University in 1987, says an object should only talk to the objects it directly knows, not to the friends of its friends. Write “bill.getTotal()”, not “bill.getLines().get(0).getItem().getPrice()”, otherwise the caller depends on the whole chain and every change in the chain breaks it.
- Check the multiplicity and the ownership with the business, and write them down, it is a business rule, not a technical detail.
Wrap up
In this article, we have looked at association, multiplicity, aggregation, composition, dependency and inheritance, how to show them in a UML class diagram and how to choose the right relationship. I really appreciate that you took the time to read this article. We have now seen every piece, one at a time. In the next and final article, I will walk you through how all these concepts connect, from the real-world problem to computational thinking to object-oriented thinking, so that you have one clear mental model in your mind.
Related Posts
How all these concepts connect? - Part 8
This final article connects the three series, showing how real-world problems, computational thinking and object-oriented principles form one practical mental model for designing software with a grocery shop example.
How do we organize what we know? - Part 2
This article explains classification, generalization, specialization and hierarchy, showing how programmers organize grocery items and discover common concepts before designing software models for beginners today.
How to model the real world with objects? - Part 1
This article introduces object-oriented thinking by modeling a grocery shop as customers, items and bills, then explains objects through identity, state, behaviour and classes for beginners.