UML and Java Implementation

Association
Association
The simplest link between classes is Association. Association, Aggregation and Composition are known as a 'has a' relationship. ClassA 'has a' ClassB.
public class ClassA {
    private ClassB classB;
}
Aggregation
Aggregation
This implies tighter coupling between classes but has no implementation difference with Association in Java.
public class ClassA{
    private ClassB classB;
}
Composition
Composition
In composition, the part object can belong to only one whole part. Also, when the whole dies the part objects die as well. Code like the example, does not mean composition, unless the previous statements are true.
public class ClassA{
    private ClassB classB = new ClassB();
}
or
public class ClassA{
    private ClassB classB;
    public ClassA(){
       classB = new ClassB();
    }
}
Realization
Realization
public class ClassA implements InterfaceA{

}
Inheritance
Inheritance
This is the super class / sub class relationship. This is some times called the 'is a' relationship. ClassB is a ClassA
public class ClassB extends ClassA{

}
Dependency
Dependency
public class ClassA{
    public someMethod(ClassB classB){
       //do something using ClassB
    }
}
or
public class ClassA{
    public someMethod(){
       ClassB classB = new ClassB();
       //do something using classB
    }
}
Anonymous Inner Class
I have not seen any UML semantics for this. However, the only place I have seen anonymous inner classes is like in the example. The inner class has local scope, therefore I believe a dependency diagram could be used. I would suggest an anonymous stereo type be used. A common time to use an anonymous inner class is to declare and implement a class as a parameter to a method.
classA.addActionListener(
    new ActionListener(){
        public void actionPerformed(ActionEvent event){
            System.exit(0);
        }
    }
);
Inner Class
More information might be communicated by using regular relationship semantics. An Inner Class stereo type should be used for the relationships.
public class ClassA{
    //other classA 'stuff'
    public class ClassB{
        //other classB 'stuff'
    }
}

Return to Java Help Page