There are five key relationships between classes in a UML class diagram : dependency, aggregation, composition, inheritance and realization. These five relationships are depicted in the above diagram:

The above relationships are read as follows:

  • Dependency : class A uses class B
  • Aggregation : class A has a class B
  • Composition : class A owns a class B
  • Inheritance : class B is a Class A  (or class A is extended by class B)
  • Realization : class B realizes Class A (or class A is realized by class B)

Dependency

dependencyDependency is represented when a reference to one class is passed in as a method parameter to another class.

public class A {

 public void doSomething(B b) {

 }

Aggregation

aggregationclass A stored the reference to class B for later use.A more common and more obvious example of Aggregation would be via setter injection:

Diamond near the parent class

public class A {

 private B _b;

 public void setB(B b) { _b = b;
 }

Composition

compositionAggregation is the weaker form of object containment.The stronger form is called Composition.In Composition the containing object is

responsible for the creation

and life cycle of the contained object

First, as a class level

public class A {

 private B _b = new B();

Second, via constructor initialization:

public class A {

 private B _b;

 public A() { // default constructor
 _b = new B();
 }

Third, via lazy init

public class A {

 private B _b;

 public void doSomethingUniqueToB() {
 if (null == _b) {
 _b = new B();
 }
 return _b.doSomething();
 } // doSomethingUniqueToB()

Inheritance

inheritanceInheritance is a fairly straightforward relationship to depict in Java:

public class A {

...

} // class A

public class B extends A {

....

} // class B

Realization

realizationRealization is also straightforward in Java and deals with implementing an interface:

public interface A {

...

} // interface A

public class B implements A {

...

} // class B
Advertisements