OOPs Concept -What is Aggreatrion in Java ?
Aggreatrion is a unique type of affiliation. It is a connection between two classes like affiliation, anyway its a directional affiliation, which implies it is stringently a single direction affiliation. It addresses a HAS-A relationship.
Aggreatrion Example in java.
For instance consider two classes Student class and Address class. Each understudy has a location so the connection among understudy and address is a Has-A relationship. In any case, on the off chance that you consider its the other way around, it would not bode well as an Address doesn't have to have a Student fundamentally. Lets compose this model in a java program.
Understudy Has-An Address
class Address
{
int streetNum;
String city;
String state;
String country;
Address(int road, String c, String st, String coun)
{
this.streetNum=street;
this.city =c;
this.state = st;
this.country = coun;
}
}
class StudentClass
{
int rollNum;
String studentName;
/Creating HAS-A relationship with Address class
Address studentAddr;
StudentClass(int move, String name, Address addr){
this.rollNum=roll;
this.studentName=name;
this.studentAddr = addr;
}
public static void main(String args[]){
Address promotion = new Address(55, "Agra", "UP", "India");
StudentClass obj = new StudentClass(123, "Chaitanya", promotion);
System.out.println(obj.rollNum);
System.out.println(obj.studentName);
System.out.println(obj.studentAddr.streetNum);
System.out.println(obj.studentAddr.city);
System.out.println(obj.studentAddr.state);
System.out.println(obj.studentAddr.country);
}
}
Output:
123
Chaitanya
55
Agra
UP
India
The above model shows the Aggregation among Student and Address classes. You can see that in Student class I have announced a property of type Address to get understudy address. Its a regular illustration of Aggregation in Java.
Why We Need Aggreation ?
To keep up code re-convenience. To comprehend this lets take a similar model once more. Assume there are two different classes College and Staff alongside over two classes Student and Address. To keep up Student's location, College Address and Staff's location we don't have to utilize a similar code over and over. We simply need to utilize the reference of Address class while characterizing every one of these classes like:
Understudy Has-An Address (Has-a connection among understudy and address)
School Has-An Address (Has-a connection among school and address)
Staff Has-An Address (Has-a connection among staff and address)
Consequently we can improve code re-convenience by utilizing Aggregation relationship.
So in the event that I need to compose this in a program, I would do it like this:
class Address
{
int streetNum;
String city;
String state;
String country;
Address(int road, String c, String st, String coun)
{
this.streetNum=street;
this.city =c;
this.state = st;
this.country = coun;
}
}
class StudentClass
{
int rollNum;
String studentName;
/Creating HAS-A relationship with Address class
Address studentAddr;
StudentClass(int move, String name, Address addr){
this.rollNum=roll;
this.studentName=name;
this.studentAddr = addr;
}
...
}
class College
{
String collegeName;
/Creating HAS-A relationship with Address class
Address collegeAddr;
College(String name, Address addr){
this.collegeName = name;
this.collegeAddr = addr;
}
...
}
class Staff
{
String employeeName;
/Creating HAS-A relationship with Address class
Address employeeAddr;
Staff(String name, Address addr){
this.employeeName = name;
this.employeeAddr = addr;
}
...
}
As you can see that we didn't compose the Address code in any of the three classes, we essentially made the HAS-A relationship with the Address class to utilize the Address code. The spot speck(… ) part in the above code can be supplanted with the public static void fundamental technique, the code in it is like what we have found in the main model.
0 Comments:
Post a Comment