Association Classes

Consider the relationship "Person X owns N shares of Company Y". Where will N be stored? N is neither an attribute of Company nor Person. In cases like this we can represent links as objects. These link objects are instances of association classes:

assoc

During the implementation phase an association class might be translated into Java as follows:

public class Company { ... }

public class Person {
   private List<Owns> invstments;
   public void add(Company c, int shares) {
      investments.add(new Owns(c, this, shares);
   }
   // etc.
}

class Owns {
   private Person owner;
   private Company company;
   private int shares;
   public Owns(Company c, Person p, int num) {
      comnpany = c;
      person = p;
      shares = num;
   }
   // etc.
}