Wednesday, 7 September 2016

Associations





Association in java :-
There are four types of association in java
1.       One –to-One Association
2.       One –to-Many Association
3.       Many-to-One Association
4.       Many-to-Many Association
In java application, if we want to achieve associations then we have to declare either single reference variable or array of reference variable of an entity class in another entity class. 
Ex :-
class Account
{
                ---------
}
class Address
{
                ---------
}
class Employee
{
                String eid;
                String ename;
                String esal;
                Account acc; //one-to-one
                Address[] addr; //one-to-many
                ------


}






In object oriented programming, there are two types of Associations.
1.       Composition
2.       Aggregation
Q) What are the difference between composition and aggregation? 
Ans):-
1.       Composition is representing strong association between entities.
Aggregation is representation weak association between entities.
2.       In case of composition, if we destroy container object then automatically contained object is also be destroyed.
In case of Aggregation, if we destroy container object then contained object my not be destroyed.
3.       In case of composition, contained object lifetime is completely depending on the container object lifetime.
In case of aggregation, container object lifetime is not depending on container object lifetime.

Example :-
If we take entities like library, books and student then library is container, books and student are contained objects. If we close library then there is no chance for books to exit, but there is a chance for student to exit in class room or in playground. In this context, the association between library and books is strong association and it will representation composition.
The association between library and student is weak association, so that, it will represent Aggregation.






No comments:

Post a Comment