3
 ---------                  ---------
|         | *            * |         |
| Person  |________________| Company |
|         |       |        |         |
 ---------        |         ---------
                  |
            ______|______
           |             |
           |             |
           |  Employment |
           |             |
           |_____________|

I try to design a classes with association class in JAVA , but i don't understanding how to use the association class in implementation .. but I understanding the benefits for it.

this what I try to do it :

public class person {
    protected employment emp;
    .
    .
}  

public class company {
    protected employment emp;
    .
    .
}  

public class employment {
    protected person p[];
    protected company c[];
    .
    .
}  

Is the relationship and the implementation is correct ?

Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
  • You diagram is not looking well on mobile...please define the relations by text. – ZivS Jun 11 '16 at 18:46
  • 1
    Pretty similar: http://stackoverflow.com/questions/13443724/uml-how-to-implement-association-class-in-java –  Jun 11 '16 at 18:49

2 Answers2

2

No, the multiplicity is on references to employment.

Note that Java naming convention is for class names to start with uppercase letter.

Java arrays are fixed-size, so it's better to use List, so you can easily add more employments.

In general, fields should be private.

public class Person {
    private List<Employment> employments;
    .
    .
}

public class Company {
    private List<Employment> employments;
    .
    .
}

public class Employment {
    private Person person;
    private Company company;
    .
    .
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • you using the vector here ? to make adding new element is easy ? @Andreas – ZahraaJawad Jun 11 '16 at 20:11
  • I would definitely not be using `Vector`, since it has been obsolete for non-concurrent use since Java 1.2 came out in 1998. I'd use `ArrayList`. – Andreas Jun 12 '16 at 02:16
0

What do you think about:

public class person {
    private employment emp;
    .
    .
}  

public class employment {
    private person p;
    private company c;
    .
    .
}  

public class company {
    private employment[] emp;
    .
    .
}  

?

Ami Hollander
  • 2,435
  • 3
  • 29
  • 47
  • i don't understand why you doing this ? why you put the employment in the company as array and in person as object ? @Ami Hollander – ZahraaJawad Jun 11 '16 at 20:10
  • I think because the company can have multiple employees, while a Person is only employed once – Niklas S. Jun 11 '16 at 20:24
  • when i wrote employment[] i meant to a container, it can be list/arraylist or a different container. my Person had an employment object because i'd assumed that every person have only one employment. – Ami Hollander Jun 11 '16 at 20:31
  • @Ami Hollander , what if i want to make the person have different job in different company .. i make `protected employment emp[] ;` in the person class ? – ZahraaJawad Jun 12 '16 at 01:24
  • why not `private employment emp[]`? yes, it's sound correct that a person can have 2 jobs in the same time. why you use `protected`? maybe you find it more clearly here: http://www.tutorialspoint.com/java/java_access_modifiers.htm – Ami Hollander Jun 12 '16 at 07:30