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.
Association represents a βusesβ relationship where two classes can interact but still exist independently.
Output:
Aggregation is a whole-part relationship, but the part can exist independently of the whole.
Output:
Composition is also a whole-part relationship, but the part cannot exist without the whole.
Output:
If the House
object is destroyed, its Room
objects are also destroyed β strong ownership.
Inheritance means a subclass inherits the properties and methods of a superclass.
Output:
Dependency occurs when one class uses another temporarily for its functionality.
Output:
Realization is when a class implements the behavior defined in an interface.
Output:
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
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.