There are several key relationships between class and class /interface in UML class diagram.
1. Dependency
class A uses class B
Sample code
1 2 3 4 5 6 7 8 9 10 11 |
public class A { public void execute(B b) { } } class B { // ... } |
2. Aggregation
In the class diagram, Class B passes/injects a reference to Class A (via method or constructor). In the, if the reference was changed, it might affect As behavior.
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class A { private B b; public void execute(B b) { this.b = b; } } class B { // ... } |
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class A { private B b; public void execute() { this.b = new B(); } } class B { // ... } |
Sample code:
1 2 3 4 5 6 7 8 |
public class B extends A { // ... } class A { } |
Sample code:
1 2 3 4 5 6 7 8 |
public class B implements A { // ... } interface A { } |