PDA

View Full Version : Association in java 2011



saneha
04-24-2011, 09:59 PM
I have two classes named Teacher and Student. Student class will get services from Teacher class, How to Associate Teacher class with student class?. I made two Independent classes and now how to Associate?

How teacher class will refer to student class. How to establish reference in Teacher class?

and use Student object in Teacher class?

thanks.

Vuhelper
04-24-2011, 09:59 PM
In Associations are typically implemented by references, i.e. object of one class will be used in the other class.



An Aggregation is an Association which denotes an "is part of" relationship. Unfortunately, the definition of this relationship is quite lax, so basically everyone is using his own interpretation. The only definitive (?) property is that in an instance graph, aggregations are not allowed to be circular - that is, an object can not be "a part of itself". (or) When building new classes from existing classes using aggregation, a composite object built from other constituent objects that are its parts. Java supports aggregation of objects by reference, since objects can't contain other objects explicitly.



A Composition adds a lifetime responsibility to Aggregation. In a garbage collected language like Java it basically means that the whole has the responsibility of preventing the garbage collector to prematurely collect the part - for example by holding a reference to it. (In a language like C++, where you need to explicitly destroy objects, Composition is a much more important concept.) Only one whole at a time can have a composition relationship to a part, but that relationship doesn't need to last for the whole lifetime of the objects - with other words, lifetime responsibility can be handed around.