Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of classes with association in JAVA

Tags:

java

 ---------                  ---------
|         | *            * |         |
| 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 ?

like image 894
ZahraaJawad Avatar asked Sep 05 '26 01:09

ZahraaJawad


1 Answers

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;
    .
    .
}
like image 186
Andreas Avatar answered Sep 07 '26 14:09

Andreas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!