Follow us on Facebook

Header Ads

Welcome to JAVA POint

Association in Example(OPPs Concept in JAVA)

                   OPPs Concept in  (What is Association in JAVA).

In this article we will talk about Association in Java. Affiliation builds up connection between two separate classes through their articles. The relationship can be balanced, One to many, numerous to one and numerous to many.

Association in Example

class CarClass{ 

String carName; 

int carId; 

CarClass(String name, int id) 

this.carName = name; 

this.carId = id; 

class Driver expands CarClass{ 

String driverName; 

Driver(String name, String cname, int cid){ 

super(cname, cid); 

this.driverName=name; 

}

class TransportCompany{ 

public static void main(String args[]) 

Driver obj = new Driver("Andy", "Portage", 9988); 

System.out.println(obj.driverName+" is a driver of vehicle Id: "+obj.carId); 


Output: 

Andy is a driver of vehicle Id: 9988 


In the above model, there is a balanced relationship(Association) between two classes: CarClass and Driver. Both the classes address two separate substances. 


Affiliation versus Aggregation versus Composition 


Lets talk about distinction between Association, Aggregation and Composition: 

Albeit each of the three are connected terms, there are some significant contrasts in the manner in which they relate two classes. Affiliation is a connection between two separate classes and the affiliation can be of any sort say coordinated, one to may and so on It joins two altogether separate substances. 


Collection is a unique type of affiliation which is a unidirectional one route connection between classes (or substances), for example Wallet and Money classes. Wallet has Money yet cash doesn't have to have Wallet fundamentally so its a one directional relationship. In this relationship both the passages can endure if other one finishes. In our model if Wallet class is absent, it doesn't imply that the Money class can't exist. 


Structure is a confined type of Aggregation wherein two substances (or you can say classes) are exceptionally reliant upon one another. For example Human and Heart. A human requirements heart to live and a heart needs a Human body to endure. All in all when the classes (substances) are subject to one another and their life expectancy are same (in the event that one passes on, another as well) at that point its an arrangement. Heart class has no sense if Human class is absent.

0 Comments:

Post a Comment

C language and C++ progaming