In object oriented programming Association and Aggregation shows the relationship between the classes. Relationship in OOPS defines the connection between the objects. It basically shows how objects are attached to each other and how they will operate.
ASSOCIATION
Association in object oriented programming shows the relationship between two or more object where each object has its own processing and it behave differently. It is represented by a solid line.
Lets take an example of relationship between Client and Programmer. Multiple programmer can associate with a single client and a single client can associate with multiple programmer . Both Client and Programmer have their own life-cycle. Both can be created and deleted independently.
class Bike{
String bikename;
int bikeId;
double bikespeed;
Bike(string name,double speed,int Id)
{
this.bikename=name;
this.bikespeed=speed;
this.bikeId=Id;
}
class Driver{
String driverName;
int driverAge;
Driver(String name, int age){
this.driverName=name;
this.driverAge=age;
}
}
class Transport{
public static void main(String args[])
{
bikeClass obj= new bikeClass("FZ", 134.15, 3452);
Driver obj2 = new Driver("Jack", 24);
System.out.println(obj2.driverName+" is a driver of bike Id: "+obj.bikeId);
}
}
output
Jack is a driver of bike Id : 3452
Aggregation
Aggregation in Object Oriented Programming is a specialized form of association in which each object have its own life cycle. Parent object does not belong to child object Example of Client and Programmer. A client may belong to multiple programmer.Hence client is a part of multiple client . But if we delete a Programmer Client object will not destroy.
class address
{
int Id;
String city;
String state;
String country;
address(int i, String c, String st, String cn)
{
this.i=Id;
this.city =c;
this.state = st;
this.country = cn;
}
}
class student
{
int roll;
String studname;
Address studAddr;
student(int rolln, String name, Address addr){
this.roll=rolln;
this.studname=name;
this.studAddr = addr;
}
public static void main(String args[]){
address ad = new Address(ii/24, "punjab", "UP", "India");
student obj = new student(66, "Jack", wertg);
System.out.println(obj.rollNum);
System.out.println(obj.studname);
System.out.println(obj.studAddr.streetNum);
System.out.println(obj.studAddr.city);
System.out.println(obj.studAddr.state);
System.out.println(obj.studAddr.country);
}
}
Output
66
Jack
ii/24
punjab
UP
India
0 Comment(s)