A H M A D G O H A R

Please Wait For Loading

UML Class Diagram Relationships: Dependency, Aggregation, Composition, and More

UML Class Diagrams depict the structure of a system by showcasing its classes and their relationships. Understanding the five key relationshipsdependency, aggregation, composition, inheritance, and realization—is crucial for modeling and implementing systems effectively. Here’s a breakdown of these relationships with examples in Java.

image

1. Dependency: Class A Uses Class B

Definition: A dependency indicates that Class A temporarily uses Class B. This happens when an instance of Class B is passed as a parameter to a method in Class A.

Example in Java:

public class A {
    public void doSomething(B b) {
        // Class A uses Class B here
    }
}

Use Case: Dependency is useful for methods that need to interact with another class briefly without maintaining a long-term reference.


2. Aggregation: Class A Has Class B

Definition: Aggregation represents a “has-a” relationship where Class A stores a reference to Class B but does not own its lifecycle. This relationship is often established through setter methods.

Example in Java:

public class A {
    private B _b;

    public void setB(B b) {
        _b = b; // Aggregation - Class A has Class B
    }
}

Use Case: Aggregation is useful when one class requires another as part of its functionality but does not control its lifecycle.


3. Composition: Class A Owns Class B

Definition: Composition is a stronger form of aggregation. Class A owns Class B and is responsible for its creation and lifecycle.

Examples in Java:

  1. Member Initialization: public class A { private B _b = new B(); // Class A creates and owns Class B }
  2. Constructor Initialization: public class A { private B _b; public A() { _b = new B(); // Class B is created during Class A's construction } }
  3. Lazy Initialization: public class A { private B _b; public B getB() { if (_b == null) { _b = new B(); // Created only when needed } return _b; } }

Use Case: Composition is used when one class’s lifecycle is tightly bound to another.


4. Inheritance: Class B Is a Class A

Definition: Inheritance establishes an “is-a” relationship where Class B extends Class A.

Example in Java:

public class A {
    // Base class functionality
}

public class B extends A {
    // Class B inherits from Class A
}

Use Case: Inheritance is used for hierarchical relationships and shared functionality among classes.


5. Realization: Class B Realizes Class A

Definition: Realization occurs when a class implements an interface. This indicates that Class B promises to fulfill the behavior defined by Class A.

Example in Java:

public interface A {
    void performTask();
}

public class B implements A {
    @Override
    public void performTask() {
        // Implementation of performTask
    }
}

Use Case: Realization is essential for defining and enforcing behavior across multiple classes.


Summary of UML Relationships

RelationshipMeaningExample in Java
DependencyClass A uses Class B temporarily.Pass Class B as a parameter to a method.
AggregationClass A has Class B (weaker “has-a”).Store a reference to Class B via a setter.
CompositionClass A owns Class B (stronger “has-a”).Create and manage Class B within Class A.
InheritanceClass B is a Class A.Class B extends Class A.
RealizationClass B implements the behavior of Class A.Class B implements an interface Class A.

Choosing the Right Relationship

  • Use Dependency for temporary interactions.
  • Use Aggregation for shared references without ownership.
  • Use Composition for tightly coupled lifecycle management.
  • Use Inheritance for shared functionality in hierarchical structures.
  • Use Realization to enforce behavior contracts.

Conclusion

Understanding these relationships in UML class diagrams is critical for modeling real-world systems effectively. By mapping these concepts to Java implementations, developers can design scalable and maintainable systems.

Would you like detailed UML diagrams or additional Java examples for these relationships? 😊

author avatar
Ahmad Gohar
With over 18 years of experience in software architecture, Java technologies, and leadership, I specialize in crafting scalable, future-proof solutions for global organizations. Whether it’s transforming legacy systems, building cutting-edge cloud-native applications, or mentoring teams to excel, I’m committed to delivering value-driven results.

Leave A Comment