3
votes
class ClassA {
    private JFrame frame = new JFrame(); // has-a, composition 

    public ClassA() 
    {
       frame.add(new ClassB());
    }       
}

is there a name for the relationship/association between ClassA and Classb? ClassA does not have a field for ClassB.

I have read about class associations in "Introduction to Java Programming" by Y. Daniel Liang and used google, but can't find an example with this specific relation.

i'm drawing an UML diagram of some existing code and want to get the correct relation annotation.

Maybe i'm overthinking it and it's just simply an association?

2

2 Answers

3
votes

Your instances of ClassB are memorized through a JFrame so you can use a class-relation :

enter image description here

or just add a dependency from ClassA to ClassB

1
votes

A private attribute is usually modeled as a unidirectional Association (even though navigability could also be achieved by other means than an owned attribute).

Your source code defines attribute frame, therefore, you should model an Association between ClassA and JFrame.

Then class JFrame has an add operation for a ClassB object. This makes only sense, if JFrame has an Association to ClassB.

Finally ClassA creates a new instance of ClassB. This fact can be visualized by a «create» Relationship (a dashed open arrow).

No other relationship between ClassA and ClassB exists. Since we don’t know, whether JFrames attribute is private, it might even be impossible for ClassA to access ClassB, even though it created it.

A Composition could exist between JFrame and ClassB, but your Source Code doesn’t give enough context for this decision. It makes no sense between ClassA and ClassB, since they don’t even are associated, be it by an AssociationClass and by a normal Association.