Saturday, August 22, 2020

Association in Java Definition and Examples

Relationship in Java Definition and Examples The affiliation relationship demonstrates that a class thinks about, and holds a reference to, another class. Affiliations can be portrayed as a has-a relationship on the grounds that the regular execution in Java is using an example field. The relationship can be bi-directional with each class holding a reference to the next. Accumulation and creation are sorts of affiliation connections. Affiliations go along with at least one of one thing against at least one of something else. A teacher may be related with a school course (a balanced relationship) yet additionally with every understudy in her group (a one-to-numerous relationship). The understudies in a single area may be related with the understudies in another segment of a similar course (a many-to-numerous relationship) while all the segments of the course identify with a solitary course (a many-to-one relationship). Affiliation Example Envision a straightforward war game with an AntiAircraftGun class and a Bomber class. The two classes should know about one another in light of the fact that they are intended to devastate one another: open class AntiAirCraftGun { Â â private Bomber target; Â â private int positionX; Â â private int positionY; Â â private int harm; Â â public void setTarget(Bomber newTarget) Â â { Â â â â this.target newTarget; Â â } Â â //rest of AntiAircraftGun class } open class Bomber { Â â private AntiAirCraftGun target; Â â private int positionX; Â â private int positionY; Â â private int harm; Â â public void setTarget(AntiAirCraftGun newTarget) Â â { Â â â â this.target newTarget; Â â } Â â //rest of Bomber class } The AntiAirCraftGun class has-a Bomber object and the Bomber class has-an AntiAirCraftGun object.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.