When designing object-oriented systems, relationships between classes form the backbone of your architecture. UML (Unified Modeling Language) provides different ways to represent these relationships. In Java, we can demonstrate these relationships with code examples.

In this blog, weβll explore:
-
Association
-
Aggregation
-
Composition
-
Inheritance
-
Dependency
-
Realization
Each concept is explained with Java code and represented in a UML diagram.
1. Association β Objects are related but independent
Association represents a βusesβ relationship where two classes can interact but still exist independently.
Example:
Output:
2. Aggregation β Weak "has-a" relationship
Aggregation is a whole-part relationship, but the part can exist independently of the whole.
Example:
Output:
3. Composition β Strong "has-a" relationship
Composition is also a whole-part relationship, but the part cannot exist without the whole.
Example:
Output:
If the House object is destroyed, its Room objects are also destroyed β strong ownership.
4. Inheritance β βis-aβ relationship
Inheritance means a subclass inherits the properties and methods of a superclass.
Example:
Output:
5. Dependency β One class depends on another
Dependency occurs when one class uses another temporarily for its functionality.
Example:
Output:
6. Realization β Implementing an Interface
Realization is when a class implements the behavior defined in an interface.
Example:
Output:
UML Diagram of All Relationships
Hereβs a UML diagram that summarizes these six relationships:
Association β Teacher β Student
Aggregation β Department β Professor
Composition β House β Room
Inheritance β Dog β Animal
Dependency β Printer β Document
Realization β Cat β’ AnimalActions
Conclusion
We explored six fundamental UML/OOP relationships with Java examples:
-
Association β Objects are related but independent.
-
Aggregation β Weak βhas-aβ relationship, parts can exist independently.
-
Composition β Strong βhas-aβ relationship, parts die with the whole.
-
Inheritance β βIs-aβ relationship between superclass and subclass.
-
Dependency β One class temporarily depends on another.
-
Realization β Class implements interface behavior.
These relationships are the building blocks for designing clean, extensible, and maintainable software systems.