Saturday, August 22, 2020

Association in Java Definition and Examples

Relationship in Java Definition and Examples The affiliation relationship shows that a class thinks about, and holds a reference to, another class. Affiliations can be depicted as a has-a relationship in light of the fact that the normal execution in Java is using an example field. The relationship can be bi-directional with each class holding a reference to the next. Collection and sythesis are kinds of affiliation connections. Affiliations go along with at least one of one thing against at least one of something else. An educator may be related with a school course (a balanced relationship) yet in addition 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 basic war game with an AntiAircraftGun class and a Bomber class. The two classes should know about one another on the grounds that they are intended to demolish 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.